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

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)

Related

syntax error near unexpected token `if' in Bash Scripting

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

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//')

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

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

UNIX shell script error - syntax error near unexpected token `if'

I am getting error "syntax error near unexpected token `if'" while executing the shell script.
Could someone help me find out if I am doing some mistake syntactically.
if [ date "+%b %d" == ls -lrt start.log | tr -s " " | cut -d" " -f6-7 ] &&
[ date "+%k" == ls -lrt start.log | tr -s " " | cut -d" " -f8 | cut -c1-2 ] &&
[ ls -lrt start.log | tr -s " " | cut -d" " -f8 | cut -c4-5 >= date -d "now 15 minutes ago" "+%M" ]; then
You need to use command substitutions to capture the output of the commands for comparison.
if [ $(date "+%b %d") = $(ls -lrt start.log | tr -s " " | cut -d" " -f6-7) ] &&
# etc.

operand expected (Error token is "-") and ambiguous redirect

I have to errors, operand expected and ambiguous redirect.
Here's my code:
#!/bin/bash
read input >| inputfile
file_name=$(cut -d" " -f1 inputfile)
i=$(cut -d" " -f2 inputfile)
j=$(cut -d" " -f3 inputfile)
k=$(cut -d" " -f4 inputfile)
l=$(cut -d" " -f5 inputfile)
maxlinetoget=$[$l-$k]
currentlinecount=1
result=0
while read line
do
if [ $currentlinecount -ge $k && $currentlinecount -le $l ]
then
echo -n $line >| linefile
echo -n $line
for number in linefile
do
echo $number
result=$[$result+$number]
done
fi
currentlinecount=$[$currentlinecount+1]
done < $file_name
echo $result
And the errors:
./P4.4: line 8: -: syntax error: operand expected (error token is "-")
./P4.4: line 24: $file_name: ambiguous redirect
line 8 is: maxlinetoget=$[$l-$k]
line 24 is: done < $file_name
I have no idea what's wrong, please tell me.
Thanks.
Both errors are the result of the fact that neither $l not $file_name have a value. Bash sometimes produces mysterious error messages when unquoted variables are empty. (You would have gotten more sensible error messages if you'd used $((l-k)) instead of the deprecated $[$l-$k], or had quoted your substitutions, particularly "$file_name".)
read line >| inputfile
reads one line from stdin and puts it in the variable $line. It produces no output, so inputfile is empty. Consequently, all of the following cut commands produce no output (nothing-in, nothing-out: the infamous NINO).
What you apparently wanted to do was
read file_name i j k l rest
((maxlinetoget = l - k))
# Or maxlinetoget=$((l-k))

Resources