syntax error near unexpected token `fi' Linux [duplicate] - linux

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 6 years ago.
I had to try to make a code to automate steamcmd but it always gives me the following error:
syntax error near unexpected token 'fi'
Code:
STEAMCMDDOWN="https://steamcdn-a.akamihd.net/client/installer/steamcmd_linux.tar.gz"
STEAMDIR="~/steamcmd"
if [! -d "$STEAMDIR" ]; then
mkdir "~/steamcmd"
cd "~/steamcmd"
else
if [! -f "steamcmd.sh" ]; then
wget "$STEAMCMDDOWN"
tar +xf "steamcmd_linux.tar.gz"
else
echo "steamcmd installed"
fi
exit
fi
Can someone please explain why I'm getting this error?

[ is a command (synonym for test), so you need to have some space after it:
if [ ! -d "$STEAMDIR" ]; then ...

Related

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

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

Shell script which prints error message when package not found [duplicate]

This question already has answers here:
How do I suppress shell script error messages?
(6 answers)
Detect if executable file is on user's PATH [duplicate]
(7 answers)
Closed 1 year ago.
I'm writing a shell script, and I need to check for some dependencies being installed before executing anything. I found I can use which <package> to see if it is installed or not. The problem is that when that dependency is not found, it throws the following error into console's output:
which: no abc in (/home/pace/.emacs.d/bin:/usr/local/bin:/home/pace/.emacs.d/bin:/usr/local/bin:/home/pace/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:...)
I want to avoid having such output, as I already have error messages shown when something fails. How can I avoid which from writing anything?
function is_installed() {
if [[ ! $(which $1) ]]
then
echo "[ERROR]: $1 $2"
exit 1
fi
}
Well, there might be better ways to do what you're trying to do (I'm not certain of the "best" way), but you can redirect stderr and stdout to hide the results from the output:
function is_installed() {
if [[ ! $(which $1 > /dev/null 2>&1 ) ]]
then
echo "[ERROR]: $1 $2"
exit 1
fi
}
(recent versions of bash support using >& /dev/null too to do both at once, but the above is slightly more portable)
EDIT -- try this instead
function is_installed() {
which $1 > /dev/null 2>&1
if [ $? = 1 ] ; then
echo "[ERROR]: $1 $2"
exit 1
fi
}

get " [: if-then: unexpected operator ",when searching all sub-folder in a folder [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 4 years ago.
when I run this script in my Ubuntu:
for item in *
do
if [ -d $item ]
then
echo $item
fi
done
I got this output:
output
I don't understand why I get "[: if-then: unexpected operator"
Escape your variables. Read about it here.
for item in *
do
if [ -d "$item" ]
then
echo "$item"
fi
done

how I could get a echo output if file is empty in linux bash? [duplicate]

This question already has answers here:
How to check if a file is empty in Bash?
(11 answers)
Closed 5 years ago.
I have a simple command for identify when file is empty but not works
MN=$(echo "$(cat empty)") ;
if [ MN == "" ]; then echo "This file is empty"; else echo "This file has been edited. You'll need to do it manually."; fi
whats I doing wrong
Please help me
test -s tests whether a file exists and is nonempty.
if test -e empty && ! test -s empty; then
echo "This file exists but is empty"
fi

Cannot stat mv, file not found [duplicate]

This question already has answers here:
Tilde in path doesn't expand to home directory
(5 answers)
Closed 8 years ago.
could somebody please help me find the problem with this code? getting this error "mv: cannot stat '~Desktop/RecyclingBin/testtest' : No such file or directory. It does exist and it is in the location ~Desktop/RecyclingBin/testtest
fileName=$1
fileLocation='cat ~/Desktop/RecyclingBin/logs/$fileName
if [ -z "$1" ]
then
echo "please enter a valid filename"
else
echo "do you want to restore?"
read ans
if [ "$ans" =="y" ]
then
mv "~/Desktop/RecyclingBin/$fileName" "$fileLocation"
fi
fi
The quotes prevent expansion of ~. Put it outside the quotes:
mv ~/Desktop/RecyclingBin/"$fileName" "$fileLocation"

Resources