I have these if conditions, but it has compile errors. How can I fix it?
if [ $DEVICE_ID == "" ]; then
I get error:
line 63: [: ==: unary operator expected
if [ 'ls -l Mytest*.log | wc -l' -eq 1 ]; then
i get error:
line 68: [: ls -l Kernel*.log | wc -l: integer expression expected
Quote the variable:
if [ "$DEVICE_ID" == "" ]; then
But it would be better to do:
if [ -z "$DEVICE_ID" ];
The second error is that you need to use backquotes:
if [ $(ls -l Mytest*.log | wc -l) -eq 1 ]; then
If you're using bash, use double brackets for conditional expressions: they are smarter about unquoted variables
if [[ $DEVICE_ID = "" ]]; then ...
would work (note: = instead of == for plain string equality instead of pattern matching)
For presence of files use an array
shopt -s nullglob
files=( *.log )
if (( ${#files[#]} > 0 )); the. Echo "there are log files"; fi
Related
I am not sure why my code below generates this error (standard_in) 1: syntax error -bash: [: -eq: unary operator expected . Can someone please help me figure out the problem here? Thanks!
#!/bin/bash
BAMLINES=4.47264e+09
FQ1LINES=4000000
FQ2LINES=4000000
DEBUG=1
if [ ! -z ${DEBUG} ]; then
echo "${BAMLINES} lines in .bam"
echo "${FQ1LINES} lines in all ${FQ_OUT1} files"
echo "${FQ2LINES} lines in all ${FQ_OUT2} files"
if [ $(echo "scale=2;${FQ1LINES}/${BAMLINES} > 0.40" | bc) -eq 0 ]; then
echo "Warning, FQ1 file contains ${FQ1LINES} lines - less than 40% of the number of reads of .bam file"
fi
if [ $(echo "scale=2;${FQ2LINES}/${BAMLINES} > 0.4" | bc) -eq 0 ]; then
echo "Warning, FQ2 file contains ${FQ2LINES} lines - less than 40% of the number of reads of .bam file"
fi
fi
$ echo "scale=2;($FQ1LINES/$BAMLINES) > 0.40"
scale=2;(4000000/) > 0.40
# ..............^^
You want to use either BAMLINE or BAMLINES but not both.
$ echo "scale=2;($FQ1LINES/$BAMLINES) > 0.40" | bc
(standard_in) 1: parse error
Because of that error, the output of $(echo ... | bc) is empty, and then [ only gets 2 arguments. When [ gets 2 arguments, the first operator is expected to be a unary operator (like -z is) -- -eq is not a unary operator.
You need to quote any variables/command expansions within [...]. In this case you'd get a different but more meaningful error:
$ [ "$(echo "scale=2;${FQ1LINES}/${BAMLINES} > 0.40" | bc)" -eq 0 ]
(standard_in) 1: parse error
bash: [: : integer expression expected
Or use [[...]] and you'll just see the bc error
$ [[ $(echo "scale=2;${FQ1LINES}/${BAMLINES} > 0.40" | bc) -eq 0 ]]
(standard_in) 1: parse error
I have this code here.
But I get the error:
./concatconvert: line 9: [: -eq: unary operator expected
./concatconvert: line 18: [: -eq: unary operator expected
This is my code:
#!/bin/bash
param=$#
if [ $# -le 2 ]
then
echo "Usage: concatconvert [-u|-1] FILE ...
Description: concatenates FILE(s) to standard output separating them with divider -----. Optional first argument -u or -l converts contents to uppercase or lowercase,$
fi
if [ $1 -eq "-u" ]
then
while [ $param -ge 1 ]
do
./concat | awk '{print toupper($0)}'
param=$(( param-1 ))
shift
done
fi
if [ $1 -eq "-l" ]
then
while [ $param -ge 1 ]
do
./concat | awk '{print tolower($0)}'
param=$(( param-1 ))
shift
done
fi
Why am I getting this error? I thought that -eq is a unary operator?
You have missed few things eg--> " for echo command was NOT closed. Then in if condition since you are comparing a string so change it to if [[ "$1" = "-u" ]] so following could be the script(I haven't tested it since no samples were there).
#!/bin/bash
param=$#
if [ $# -le 2 ]
then
echo "Usage: concatconvert [-u|-1] FILE ...
Description: concatenates FILE(s) to standard output separating them with divider -----. Optional first argument -u or -l converts contents to uppercase or lowercase,$"
fi
if [[ "$1" = "-u" ]]
then
while [ $param -ge 1 ]
do
./concat | awk '{print toupper($0)}'
param=$(( param-1 ))
shift
done
fi
if [[ $1 -eq -l ]]
then
while [ $param -ge 1 ]
do
./concat | awk '{print tolower($0)}'
param=$(( param-1 ))
shift
done
fi
I'm trying to get the occurrences of "(" in a variable and then use it in a loop
Tried:
LPC=$(grep -o "(" <<< $A | wc -l)
while [ LPC -gt 0 ]; do
output:
line 53: [: LPC: integer expression expected
I can't seem to get it to work.
You forgot the dollar sign before the variable.
Modify:
while [ LPC -gt 0 ]; do
to:
while [ $LPC -gt 0 ]; do
You need to perform parameter substitution on LPC:
while [ $LPC -gt 0 ]; do
I have the following instruction:
if [[ ! `wc -l <<< $SILENT_LOG` -eq 1 -o ! `wc -l <<< $ACTIVE_LOG` -eq 1 ]] then
echo "There should be only a silent report log file!" >> $DEST_DIR/$RESULT_FILE
OK=0;
fi
OK should be set to 0 when SILENT_LOG or ACTIVE_LOG have 0 or more lines. The problem is that the following error is raised:
**./reportComparator.sh: line 26: syntax error near `-o'
./reportComparator.sh: line 26: `if [[ ! `wc -l <<< $SILENT_LOG` -eq 1 -o ! `wc -l <<< $ACTIVE_LOG` -eq 1 ]] then'**
Also, I tried to replace wc -l <<< $SILENT_LOG with echo $SILENT_LOG | wc-l but it still does not work.
First, the following syntax will not work:
$ [[ 1 -eq 1 -o 2 -eq 2 ]]
bash: syntax error in conditional expression
bash: syntax error near `-o'
In bash, you have two choices:
$ [[ 1 -eq 1 || 2 -eq 2 ]]
$ [ 1 -eq 1 -o 2 -eq 2 ]
For compatibility with plain POSIX shells, neither of the above should be used, the former because plain POSIX does not support [[ and the latter because plain POSIX shells cannot parse such long tests reliably. Instead use:
$ [ 1 -eq 1 ] || [ 2 -eq 2 ]
For the second issue, let's create a variable with three lines:
$ silent_log='one
> two
> three'
Now observe:
$ wc -l <<<$silent_log
1
$ wc -l <<<"$silent_log"
3
If you want the lines counted correctly, you need to double-quote your variables.
You have some syntax issues in your code.
For [[...] you can use:
if [[ $(wc -l < "$SILENT_LOG") -eq 1 || $(wc -l < "$ACTIVE_LOG") -eq 1 ]]; then
echo "There should be only a silent report log file!" >> $DEST_DIR/$RESULT_FILE
OK=0
fi
I'm trying to write a shell script, but it's giving me a syntax error at the following command:
if [[ -n ${array[$x1]} -a [ expr length "$x1" -gt 2 ] ]]
This is the error message:
./project: line 45: syntax error in conditional expression
./project: line 45: syntax error near `-a'
./project: line 45: ` if [[ -n ${array[$x1]} -a [ expr length "$x1" -gt 2 ] ]]'
What am I doing wrong?
Use && not -a in [[ ]]
Also, expr length won't do what you expect here. The better approach, since you're already using bash extensions, is to use the ${#param} expansion to get the length of $param, and evaluate that within a math context, like so:
if [[ -n ${array[$x1]} ]] && (( ${#x1} > 2 )); then
...
fi