syntax error near unexpected token `if' in Bash Scripting - bash

Attempting to locate all lines in a list of files containing jane, then test if the result exists as a real file, and append their names to a Document. Here is my code:
#!/bin/bash
> oldFiles.txt
janeSearch=$( grep " jane " ../data/list.txt | cut -d ' ' -f 1,3 )
for x in $janeSearch;
if test -e ~/$x: then
echo $x >> oldFiles.txt
fi
Can someone explain why I get the following error?
syntax error near unexpected token `if'

Suggesting to try the following
#!/bin/bash
> oldFiles.txt
janeSearch=$( grep " jane " ../data/list.txt | cut -d ' ' -f 1,3 )
for x in $janeSearch;
if (test -e ~/$x); then
echo $x >> oldFiles.txt
fi
done

Related

Creating file with Bash scripting for loop and if test

Problem Statement
Find files using bash script
In this section, you are going to write a script named findJane.sh within the scripts directory.
This script should catch all "jane" lines and store them in another text file called oldFiles.txt. You will complete the script using the command we practiced in earlier sections. Don't worry, we'll guide you throughout the whole process.
Navigate to /scripts directory and create a new file named findJane.sh.
Mycode
#!/bin/bash
>oldFiles.txt
files=$(grep " jane " ../data/list.txt | cut -d ' ' -f 3)
for i in $files:do
do if test -e ~/data/"$i"; then
echo "$i" >> OldFiles.txt;
else
echo "File doesn't exist"; fi
done
output now
file does not exist
file does not exist
file does not exist
it should not print nothing
and cat oldFiles.txt should return all those files with name 'jane
Where i am coding wrong
Guide
Create the text file oldFiles.txt and make sure it's empty. This oldFiles.txt file should save files with username "jane".
Now, search for all lines that contain the name "jane" and save the file names into a variable. Let's call this variable files, we will refer to it with that name later in the lab.
Since none of the files present in the file list.txt are available in the file system, check if file names present in files variable are actually present in the file system. To do this, we'll use the test command that we practiced in the previous section.
Now, iterate over the files variable and add a test expression within the loop. If the item within the files variable passes the test, add/append it to the file oldFiles.txt.
I have the same problem, just don't call the files by $in grep
#!/bin/bash
files= grep ' jane ' ../data/list.txt | cut -d ' ' -f 3
for file in files; do
if test -e ~/data/$file; then
echo $file >> oldFiles.txt;
else
echo "File dosen't exsist"; fi
done
And make the new fie .oldFiles.txt in the command line
like
user#linux: ~/program$ ./findJane.sh > oldFiles.txt
The code shall work
If you're trying to do a Qwicklabs assignment, try this
#!/bin/bash
> oldFiles.txt
files=$(grep " jane " /home/<Student-id>/data/list.txt | cut -d ' ' -f 3)
for i in $files;do
if test -e ~/$i;then
echo $i>>oldFiles.txt;
else echo "not working"; fi
done
#!/bin/bash
>oldFiles.txt
files=$(grep " jane " ../data/list.txt | cut -d ' ' -f 3)
for i in $files; do
if test -e ~/$i; then
echo $i >> OldFiles.txt;
else
echo "File doesn't exist"; fi
done
you have just some typos. this is "working" code for your example
#!/bin/bash
>oldFiles.txt
files=$(grep " jane " ../data/list.txt | cut -d ' ' -f 3)
for i in $files; do
if test -e ~/data/"$i"; then
echo "$i" >> OldFiles.txt;
else
echo "File doesn't exist"; fi
done
however, this works only if list.txt doesn't contain any spaces except as delimiters between (at least) three columns
it won't work for Jane because it doesn't match jane
it won't work when there are more spaces like jane example.doc
it won't work for file names containing spaces like best movies.txt
it makes less sense to show how to do it right because the concept of list.txt is bad choice
#!/bin/bash
>oldFiles.txt
files=$(grep " jane " ../data/list.txt | cut -d ' ' -f 3)
for file in $files; do
if [ -e $HOME$file ] ; then
echo $HOME$file >> oldFiles.txt;
fi
done
#!/bin/bash
> oldFiles.txt
files=$(grep ' jane ' ../data/list.txt | cut -d ' ' -f 3)
#echo $files
for file in $files; do
if test -e ~/$file; then
echo "File path added !!"
echo $file >> oldFiles.txt;
fi
done
You need to check the path and then append it to the oldFile
#1/bin/bash
>oldFiles.txt
files=$(grep "jane " ../data/list.txt | cut -d' ' -f3)
for f in $files; do
if [ -e $HOME$f ];then
echo $HOME$f >> oldFiles.txt;
fi
done
I made a few fixes to your code and now works 😉
#!/bin/bash
> oldFiles.txt
files=$(grep " jane " ../data/list.txt | cut -d " " -f 3)
for file in $files; do
if test -e ~/$file; then echo $file >> oldFiles.txt; else echo "File doesn't exist"; fi done

Bash script expression syntax crash in loop

im have a script which puts the output of "df" into a string.
When the script runs once everything is fine, but when i put it in a while loop, it crashs in the second run with the following error.
cut: invalid field value ‘\n2\n3\n4\n5\n6’
Try 'cut --help' for more information.
./getdata.sh: line 248: 1
2
3
4
5
6%6: syntax error in expression (error token is "2
3
4
5
6%6")
Code snippet:
PARTITION_RETURN_STRING=""
PARTITION_TEMP_STRING=""
for i in $(df | awk '{ print $6 }')
do
if [ "$i" != "Mounted" ]; then
for abc in $(seq 1 6)
do
PARTITION_TEMP_STRING=$(df -m | awk -v bla=$i '$6 == bla' | tr -s ' ' | cut -d ' ' -f $abc)
PARTITION_RETURN_STRING="$PARTITION_RETURN_STRING$PARTITION_TEMP_STRING"
if [ "$(($abc%6))" = "0" ]; then
PARTITION_RETURN_STRING+="|"
else
PARTITION_RETURN_STRING+="#"
fi
done
fi
done
Seems like there is a value which is getting used in the first run and didnt resets for the second run, but i couldnt figure it out.

Errors in bash script. Syntax error near unexpected token

Do you know what is wrong with my script as I always get the error mesage:
position frac1 frac2
: command not found:
'/s1_met.sh: line 3: syntax error near unexpected token `do
'/s1_met.sh: line 3: `for lineF1 in $(cat $1); do
Code here:
export IFS=$'\n'
echo "position frac1 frac2";
for lineF1 in $(cat $1); do
if [ $(echo $lineF1 | cut -b 1-2) = "##" ]; then
echo "skip line" >&2;
else
startF1=$(echo $lineF1 | cut -f 4);
stopF1=$(echo $lineF1 | cut -f 5);
fracF1=$(echo $lineF1 | cut -f 9 | cut -d ";" -f 4 | cut -d "=" -f 2);
lineF2=$(grep "$startF1" $2);
if [ -z "$lineF2" ]; then
echo "position $startF1 cannot be found" >&2;
else
fracF2=$(echo $lineF2 | cut -f 9 | cut -d ";" -f 4 | cut -d "=" -f 2);
echo "$startF1 $fracF1 $fracF2";
fi;
fi
done;
There's nothing wrong with it, you must not be running it with BASH.
Edited to say you need to check your line endings, your comment below with the ^M means that you have extra characters on the line. See here.
https://stackoverflow.com/tags/bash/info
Try putting the "shebang" line in the script shebang docs
To do this, run which bash which will tell you something like /bin/bash. Your script should then be:
#!/bin/bash
echo "I'm running with bash!"
Try that, your syntax is OK.

script to clear file contents using sed

I am trying to delete few lines (that are 1 day older) from a file using sed but it gives an error while executing the script.
What could be causing the above error? Could anyone please help?
~]# ./test.sh
Jan 20
36
sed: -e expression #1, char 3: unexpected `,'
Here is the script:
month=$(date --date="1 day ago" | cut -d " " -f2,3)
echo $month
line=$(grep -n "$month" test.log | cut -d : -f 1 | tail -1)
echo $line
if [ ! -z "$line" -a "$line" != " " ];
then
sed -i '1,"$line"d' test.log
#echo "sed -i '1,"$line"d' test.log"
else
exit
fi
I suggest you to change the sed line as,
sed -i '1,'"$line"'d' test.log
^ ^
| |

Too many arguments error in shell script

I am trying a simple shell script like the following:
#!/bin/bash
up_cap=$( cat result.txt | cut -d ":" -f 6,7 | sort -n | cut -d " " -f 2 | sort -n)
down_cap=$( cat result.txt | cut -d : -f 6,7 | sort -n | cut -d " " -f 6| sort -n)
for value in "${down_cap[#]}";do
if [ $value > 80000 ]; then
cat result.txt | grep -B 1 "$value"
fi
done
echo " All done, exiting"
when I execute the above script as ./script.sh, I get the error:
./script.sh: line 5: [: too many arguments
All done, exiting
I have googled enough, and still not able to rectify this.
You want
if [ "$value" -gt 80000 ]; then
You use -gt for checking if A is bigger than B, not >. The quotation marks I merely added to prevent the script from failing in case $value is empty.
Try to declare variable $value explicitly:
declare -i value
So, with the dominikh's and mine additions the code should look like this:
#!/bin/bash
up_cap=$( cat result.txt | cut -d ":" -f 6,7 | sort -n | cut -d " " -f 2 | sort -n)
down_cap=$( cat result.txt | cut -d : -f 6,7 | sort -n | cut -d " " -f 6| sort -n)
for value in "${down_cap[#]}";do
declare -i value
if [ $value -gt 80000 ]; then
cat result.txt | grep -B 1 "$value"
fi
done
echo " All done, exiting"

Resources