for-loop not working: trying to turn a ls into an array - bash

I'm receiving a snytax error when I run the following code:
#!/bin/bash
for i in (`ls *.nexus`);
do
awk 'NR >5' /path/to/nexus_files/$i | tr -d "'" | tr " " "\n" | sed 's/uce/>uce/g' > /path/to/fasta_files/${i}.fasta
done
error:
-bash: syntax error near unexpected token `(
when I remove parentheses:
-bash: syntax error near unexpected token 'awk'

In your simple example, you can do w/o the ls command
for i in *.nexus ; do
awk ...
done

Related

Bash issue with unexpected token

The script below seems to work when I run it on its own in vscode it says literal carriage return on the second and third line.
When I put it into a larger script I get this issue
./script.sh: line 64: syntax error near unexpected token `newline'
./script.sh: line 64: `ipv6address=$(hostname -I | cut -d " " -f 2)'
Can any one advise please
#!/bin/bash
ipv6address=$(hostname -I | cut -d " " -f 2)
tee -a <<EOF >/dev/null "$HOME"/ipv6
[network]
# replace the ip with yours
routable_ip ="$ipv6address"
EOF

Unable to get value from file

I try to get the value of "length" of the first line of different text files but I am getting this error :
")syntax error: invalid arithmetic operator (error token is "
Here is the first line of one file :
><some info> <some info> | <other info> | <otherinfo> [Source:xxxx/xxxx;xxxx:xxxx] | <some info> | length=2812
So I want to get the value 2812. After many tests, the best I can get is this :
for file in ./*.txt
do
LLINE=$(head -n 1 "$file" | awk -F "length=" '{print $NF}')
echo "${LLINE}."
if [[ "${LLINE}" -le 1500 ]];
then
<some code>
else
<some code>
fi
done
My output is :
2812
")syntax error: invalid arithmetic operator (error token is "
376
")syntax error: invalid arithmetic operator (error token is "
...
What is the issue ?
maybe grep would be better for LLINE?
grep -Eo '[0-9]{1,4}'
Finally found with all your help !
This was DOS line endings error since those files were generated by someone using windows.
I just added a sed command to get rid of this :
LLINE=$(head -n 1 "$file" | awk -F "length=" '{print $NF}' | sed $'s/\r//')

syntax error: operand expected (error token is " ")

syntax error: operand expected (error token is " ")
I'm getting this syntax error with my current code:
log= who | grep $1 | cut -c 30-31,33-34
echo $log
time= date | cut -c 12-13,15-16
echo $time
on=$(($time - $log))
echo $on
If I remember correctly, " " stands for null. Why am I getting this?
Remove the space which was just after to = symbol and put the command inside $(), so that it would parse.
log=$(who | grep $1 | cut -c 30-31,33-34)
And,
time=$(date | cut -c 12-13,15-16)

awk: syntax error near unexpected token `('

I tried to assign the output of an awk command to a variable:
USERS=$(awk '/\/X/ {print $1}' <(w))
This line is part of the following script:
#!/bin/sh
INTERFACE=$1 # The interface which is brought up or down
STATUS=$2 # The new state of the interface
case "$STATUS" in
up) # $INTERFACE is up
if pidof dropbox; then
killall dropbox
fi
USERS=$(awk '/\/X/ {print $1}' <(w))
for user in $USERS; do
su -c "DISPLAY=$(awk '/\/X/ {print $11}' <(w)) dropboxd &" $user
done
;;
down) # $INTERFACE is down
;;
esac
However, I get the following error:
script: command substitution: line 14: syntax error near unexpected token `('
script: command substitution: line 14: `awk '/\/X/ {print $1}' <(w))'
All brackets are closed. Where is the syntax error?
I'm assuming because you are using #!/bin/sh and not #!/bin/bash that process substitution is not available (or you have a version of bash that doesn't support process subsitiution, pre 4.X.X). Switch to bash or just pipe w to your awk command:
USERS=$(w | awk '/\/X/ {print $1}')

Bash for loop error

I have a script in bash what take from LocLog a ip from collumn 8 :
#!/bin/bash
for i in $(cat /scripts/logs/LocLog | awk '{print $8}' | sort | uniq);
do
php /scripts/a.php $i;
done
The script give an error:
bash -x log
'og: line 2: syntax error near unexpected token `
'og: line 2: `for i in $(cat /scripts/logs/LocLog | awk '{print $8}' | sort | uniq);
Any ideeas?
Get rid of the semicolon at the end of the for line.

Resources