Errors in bash script. Syntax error near unexpected token - bash

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.

Related

Execute commands from shell script fails

I'm trying read a file which contains lines like this:
Run COMMAND with options "OPTIONS" and arguments "ARGUMENTS"
Then I want to execute this command with given options and arguments. For example I'd like to execute these commands:
Run pwd with options "" and arguments ""
Run ls with options "-al" and arguments "$HOME"
Run ls with options "-al" and arguments "Example: \"strange folder name\""
This is my code
#!/bin/bash
while read -r line
do
COMMAND=$(echo "$line" | cut -d" " -f 2)
OPTIONS=$(echo "$line" | cut -d" " -f 5 | tr -d '"')
ARGUMENTS=$(echo "$line" | cut -d" " -f 8)
$COMMAND $OPTIONS $ARGUMENTS
done <$1
First example is working as it should, second one is giving me error ls: cannot access $HOME: No such file or directory' and third one is not storing the name of the folder to $ARGUMENTS correctly.
second one is giving me error ls: cannot access $HOME: No such file or directory'
This is because the folder named $HOME does not exist. I am not talking about the value of $HOME variable, but the string literal. The shell does not execute the parameter expansion in your situation.
third one is not storing the name of the folder to $ARGUMENTS correctly
This is because -f 8 only extract column 8, try -f 8- to extract the 8th column and all the others until the end of line.
You can give a try to this version below:
while read -r line; do
COMMAND=$(printf "%s" "${line}" | cut -d" " -f 2)
OPTIONS=$(printf "%s" "${line}" | cut -d" " -f 5 | tr -d '"')
ARGUMENTS=$(printf "%s" "${line}" | cut -d" " -f 8-)
$COMMAND $OPTIONS "$(eval printf \"%s\" "$ARGUMENTS")"
done < "${1}"
The eval is a shell built-in command which is used to enable parameter expansion of ARGUMENTS, if applicable.
I have to warn you that the eval is usualy say risky to use.

Shell : What does this script do?

#!/bin/bash
if test $# -ne 2
then
echo "Error : Invalid number of arguments"
else
if [ -d $1 ]
then
if [[ $2 =~ ^[0-9]+$ ]]
then
ls -l $1 | while read line
do
eval "echo $line | cut -d' ' -f5" | while read ln
do
if [[ $ln -gt $2 ]]
then
echo $line
fi
done
done
else
echo $2" is not a integer"
fi
else
echo "The repertory "$1" does not exist "
fi
fi
The question was to make cpp , that works like the command cp . The script it's supposed to react correctly if we don't give 2 argument. I don't understand what this script do from line 10 .
This code is the following of this post Explain me 2 lines of this shell script.
Thanks
Without working through the code line by line and explaining it, I would point you at http://explainshell.com, which takes lines of shell code and puts commentary from the manual on each parameter.
E.g, this is part of line 12 above: http://explainshell.com/explain?cmd=echo+%24line+%7C+cut+-d%27+%27+-f5
It should help you go through it line by line and work out what is going on.
In words: It selects the lines from ls -l from a directory $1 which have a size bigger than $2.
If that code is in a file called script.sh, it is called like:
$ ./script.sh /home/user 130000
And it will print all lines of ls -l /home/user which have a size bigger than 130000.
I do not know why the eval in:
eval "echo $line | cut -d' ' -f5" | while read ln
The line will work the same as:
echo $line | cut -d' ' -f5 | while read ln

How to redirect grep to a while loop

Hi I have the following bash script code
group2=0
while read -r line
do
popAll=$line | cut -d "{" -f2 | cut -d "}" -f1 | tr -cd "," | wc -c
if [[ $popAll = 0 ]]; then
group2 = $((group2+2));
else
group2 = $((group2+popAll+1));
fi
done << (grep -w "token" "$file")
and I get the following error:
./parsingTrace: line 153: syntax error near unexpected token `('
./parsingTrace: line 153: `done << (grep -w "pop" "$file")'
I do not want to pipe grep to the while, because I want variable inside the loop to be visible outside
The problem is in this line:
done << (grep -w "token" "$file")
# ^^
You need to say < and then <(). The first one is to indicate the input for the while loop and the second one for the process substitution:
done < <(grep -w "token" "$file")
# ^ ^
Note however that there are many others things you want to check. See the comments for a discussion and paste the code in ShellCheck for more details. Also, by indicating some sample input and desired output I am sure we can find a better way to do this.

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
^ ^
| |

shell script - select CSV lines by float value

I'm stuck with a strange behavior while reading a CSV file and selecting its lines with a specific column float value.
Here's an extract from the input file.
ben#truc:$ head summary.fasta.csv
scf7180000753635;170043549;XP_001849446.1;27.72;184;2e-13;74.7
scf7180000753636;340728919;XP_003402759.1;25.78;322;8e-19;93.6
scf7180000753642;328716306;XP_003245892.1;33.51;191;7e-27;119
scf7180000753642;512919417;XP_004929373.1;43.18;132;1e-23;108
scf7180000753642;512914080;XP_004928052.1;40.16;127;5e-21;94.7
scf7180000753664;328696819;XP_003240139.1;37.99;179;2e-23;107
scf7180000753664;328696819;XP_003240139.1;26.67;30;2e-23;25.4
scf7180000753664;328703138;XP_003242103.1;31.65;218;1e-20;99.4
scf7180000753669;383855900;XP_003703448.1;68.92;74;2e-23;102
scf7180000753669;380030611;XP_003698937.1;72.06;68;3e-22;99.8
Here's my shell script code:
#!/bin/sh
echo "extracting the values"
# prepare output files
echo "" > "40_sequence_identity.csv"
echo "" > "60_sequence_identity.csv"
echo "" > "80_sequence_identity.csv"
while read -r line; do
#debug: check if line is correclty read
echo $line
#attribute each CSV column value to a variable
query=`echo $line | cut -d ';' -f1`
gi=`echo $line | cut -d ';' -f2`
refseq=`echo $line | cut -d ';' -f3`
seq_identity=`echo $line | cut -d ';' -f4`
align_length=`echo $line | cut -d ';' -f5`
evalue=`echo $line | cut -d ';' -f6`
score=`echo $line | -d ';' -f7`
#debug: check if cut command is OK
echo "seqidentity:"$seq_identity
# test float value of column 4, if superior to a threshold, write the line in a specific line
if [ $( echo "$seq_identity >= 40" | bc ) ]; then
echo "$line" >> "40_sequence_identity.csv"
fi
if [ $( echo "$seq_identity >= 60" | bc ) ]; then
echo "$line" >> "60_sequence_identity.csv"
fi
if [ $( echo "$seq_identity >= 80" | bc ) ]; then
echo "$line" >> "80_sequence_identity.csv"
fi
done < "summary.fasta.csv"
echo "DONE!"
And here's the strange outputs.
extracting the values
scf7180000753635;170043549;XP_001849446.1;27.72;184;2e-13;74.7
./create_project_directories.sh: 1: ./create_project_directories.sh: -d: not found
seqidentity:27.72
scf7180000753636;340728919;XP_003402759.1;25.78;322;8e-19;93.6
./create_project_directories.sh: 1: ./create_project_directories.sh: -d: not found
seqidentity:25.78
scf7180000753642;328716306;XP_003245892.1;33.51;191;7e-27;119
./create_project_directories.sh: 1: ./create_project_directories.sh: -d: not found
seqidentity:33.51
scf7180000753642;512919417;XP_004929373.1;43.18;132;1e-23;108
./create_project_directories.sh: 1: ./create_project_directories.sh: -d: not found
seqidentity:43.18
scf7180000753642;512914080;XP_004928052.1;40.16;127;5e-21;94.7
./create_project_directories.sh: 1: ./create_project_directories.sh: -d: not found
seqidentity:40.16
scf7180000753664;328696819;XP_003240139.1;37.99;179;2e-23;107
./create_project_directories.sh: 1: ./create_project_directories.sh: -d: not found
seqidentity:37.99
scf7180000753664;328696819;XP_003240139.1;26.67;30;2e-23;25.4
./create_project_directories.sh: 1: ./create_project_directories.sh: -d: not found
seqidentity:26.67
First, the 3 output files (blast_summary_superior_40_sequence_identity.csv ...) contain all the lines, as if the tests didn't work.
Second, the file parsing seems OK, but this strange message: -d: not found , comes from nowhere.Though, it appears before the 'echo' displaying the value of $seqidentity and is probably related to the cut command.
Any idea why I have such output ?
When I manually execute the commands in the console, this works.
But not when executing the whole script.
Thanks for your help.
You are getting Error : -d: not found because on line number 17 command is incomplete
score=`echo $line | -d ';' -f7`
So it should be :
score=$(echo $line | cut -d ';' -f7)

Resources