How do I fix this if shell statement with "[" command? [duplicate] - linux

This question already has answers here:
When are square brackets required in a Bash if statement?
(3 answers)
Closed 1 year ago.
Why do I get these errors:
-bash: [: missing `]'
grep: ): No such file or directory
grep: ]: No such file or directory
when I run this in sh or bash:
if [ \( lsusb -t | grep -q 'qmi_wwan' \) ]; then
echo 'yes'
else
echo 'no'
fi

If you want to check the exit code of the grep command, use it instead of the [ command:
if lsusb -t | grep -q 'qmi_wwan'
then
echo "Yes"
fi

Related

mv: cannot stat ‘2001010000\r’: No such file or directory [duplicate]

This question already has answers here:
Read a file line by line assigning the value to a variable [duplicate]
(10 answers)
Closed 2 years ago.
I have a file abc.out and I need to read it and export the content to the named pipe. My shell script as below:
#!/bin/ksh -x
cd /home/robert/
if [[ -s abc.out ]]; then
sleep 2
for i in `cat abc.out`
do
mv -f $i ./abc_archive/x$i
print `pwd`/abc_archive/x$i > /home/robert/pipe_abc
sleep 1
done
fi
The content of abc.out file as below:
2001010000
2001010001
2001010002
But when I run the script with the command sh abc.spt, error prompted as below:
mv: cannot stat ‘2001010000\r’: No such file or directory
How can I fix this issue, many thanks!
One option would be to pipe output through either sed or tr, to remove carriage returns:
sed -e 's/[\r\n]//g'
or
tr -d '\r' if you only want to remove \r

Echo, grep and set variable issues [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 3 years ago.
I have the following scenario
Step1
local output=$(sshpass -p ${PSSWD} ssh -tt -oStrictHostKeyChecking=no root#$IP "$(< vXU-script.sh)")
step 2
mapfile -t RIU_ARRAY < <(echo "$output" | grep "RIU-[1-4] ----> ")
the above line will grep the following line
RIU-1 ----> AB019030015 ----> 223a:c03a:261:1141:0:50:c389:22ff
step 3 (loop RIU_ARRAY)
ip=$(echo "${item}" | awk -F" " '{print $5}')
The above line will get the IP part 223a:c03a:261:1141:0:50:c389:22ff
if [ "${ip}" == "223a:c03a:261:1141:0:50:c389:22ff" ]; then
echo "#### Same"
else
echo "#### Different"
fi
The above line is always False and the following line is printed
#### Different
I expect it to be the same.
Is this because of the semi-colon or CLI console greped character encoding issues?
Most likely, the 'sshpass | ssh -tt' will result in line ending with '\r\n'.
Considering 2 options - Filtering with 'tr' will (almost) always work. But the '-tt' seems cleaner.
The '-tt' cause terminal allocation, which is injecting '\r'. If possible, try running without it.
If this is not an option, pipe output into "tr -d '\r'"
local output=$(sshpass -p ${PSSWD} ssh -tt -oStrictHostKeyChecking=no root#$IP "$(< vXU-script.sh)" | tr -d '\r')

bash file Linux Ubuntu [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I don't know why my bash file in Linux Ubuntu doesn't work.
#!/bin/bash`
echo -n "Write user name : "
read NAME
cd
if grep -c "$NAME" /etc/passwd -eq 0; then
echo "$NAME doesn't esist"
else
echo "$NAME already esist"
fi`
It gives the error:
$ bash ./name.sh
Write user name : root
grep: root: No such file or directory
/etc/passwd:3
grep: 0: No such file or directory
The code is not executing the grep in a sub-shell, and the if-condition needs to be wrapped in square brackets.
#!/bin/bash
echo -n "Write user name : "
read NAME
cd
if [ `grep -c "$NAME" /etc/passwd` -eq 0 ]; then
echo "$NAME doesn't esist"
else
echo "$NAME" already esist
fi
Output:
$ bash ./name.sh
Write user name : root
root already esist
$ bash ./name.sh
Write user name : ddd
ddd doesn't esist
You have some strange backquotes in your script which you should remove
#!/bin/bash
read -p "Write user name : " name
if grep -q "$name" /etc/passwd; then
echo "$name already exists"
else
echo "$name doesn't exist"
fi
as you can see
read can show a prompt
variables should be lowercase
the exit value of grep determines the case and no need for parenthesis or brackets
also notice that is looking for name everywhere, not just user names
because you included the shebang you can
$ chmod +x script
$ ./script

ffmpeg getting syntax error when run inside shell script only.. Why? [duplicate]

This question already has answers here:
Bash: Syntax error: redirection unexpected
(9 answers)
Closed 8 years ago.
I have the following script:
#!/bin/sh
# Use the PhiPack software on our two aligned sets of sequences...
mkdir FcFeABC
cd FcFeABC
../bin/PhiPack/Phi -f ../../Data/Real_Sequences_and_Networks/FcFeABC_alignment.fas -o -v -w 10 -g
cd -
mkdir FcL10
cd FcL10
../bin/PhiPack/Phi -f ../../Data/Real_Sequences_and_Networks/FcL10_alignment.fas -o -v -w 10 -g
cd -
# Use the PhiPack software on the simulated Datasets...
cd ../Data/Simulated_Sequences_and_Networks/Constant_Sex/Theta\ =\ 0.066/Theta\ =\ 0.066/Medium/CutSequences/;
rmus=($(ls -d *.fas))
cd -
absfiles=(../Data/Simulated_Sequences_and_Networks/Constant_Sex/Theta\ =\ 0.066/Theta\ =\ 0.066/Medium/CutSequences/*.fas)
if [ ${#rmus[#]} = ${#absfiles[#]} ]
then
mkdir ${rmus[#]}
for ((i=0; i<${#absfiles[#]}; i++));
do
cd ${rmus[$i]}
.../bin/PhiPack/Phi -f ${absfiles[$i]} -o -v -w 10 -g
cd -
done
else
echo "Error, Number of files created and files to be read differs"
fi
Which hit's an error at line 16:
./runPhiTests.sh: 16: ./runPhiTests.sh: Syntax error: "(" unexpected
Which is this line:
rmus=($(ls -d *.fas))
I don't understand why the '(' is unexpected - it's a simple assignment of the results of ls to an array.
Thanks,
Ben W.
You aren't running it with bash. You are running with /bin/sh from your shebang line #!/bin/sh.
Either run with bash explicitly bash runPhiTests.sh or fix your shebang line #!/bin/bash.
Try to use #!/bin/bash instead of sh.

Which command is used for checking if file is not open in linux? [duplicate]

This question already has answers here:
How find out which process is using a file in Linux?
(4 answers)
Closed 3 years ago.
lsof command is used fot the open files in linux.Which command is used for checking if file is not open.I want to use in my script
my condition is
do
if [[ 'lsof | grep $r_error_file' ]]
then
error_text=$error_text$(tail -n +1 $r_error_file | grep 'Error\')
mv $r_error_file $(dirname ${r_error_file})/BkError/$(filename ${r_error_file})
fi
done
Use fuser command
fuser $filename
if [ $? ne 0 ]
then
# file is open, Add your code here
fi
You need the case where the lsof statement is false. There's a useful list of methods for manipulating truth in if statements here; but to ruin your fun serarching, you're looking for
if [[ ! `lsof | grep $r_error_file` ]]
then
...
fi

Resources