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

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

Related

How to validate user input on a bash script? [duplicate]

This question already has answers here:
Validate the number of arguments passed in bash from read
(2 answers)
Closed 2 years ago.
I'm creating a bash script. However, I need the script to validate that the user has to enter two arguments. Not 1 and nothing more than 3.
echo -n "Enter two values"
read val1 val2
try this:
#!/bin/bash
if [ $# -eq 2 ]; then
echo "var1: $1 var2: $2"
else
echo "error: need to set two argument"
fi
run:
./scriptName.sh first second

I am trying to write a shell script to read username from a file, but it is not working. I am posting script I am writing and output [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.
#!/bin/bash
if [ -f "$1" ]
then
for users in 'cat $1'
do
useradd $users
done
else
echo "input is not a file"
fi
You just have to get the input for the do loop right:
#!/bin/bash
if [ -f "$1" ]
then
for user in $(cat "$1")
do
useradd "$user"
done
else
echo "input is not a file"
fi
Remarks: this works for reading out a file word-by-word and I tried to keep your structure.
For reading out files line by line this is an elegant way: https://stackoverflow.com/a/4642213/2819581

Basic if else scripting [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Getting "command not found" error while comparing two strings in Bash
(4 answers)
Command not found when comparing string
(1 answer)
"Command not found" when attempting integer equality in bash
(3 answers)
Why does [$value -lt 10] in shell result in command not found? [duplicate]
(3 answers)
Closed 4 years ago.
I hope you all are doing great. I am new to Linux shell scripting. I am trying to learn basic shell scripting and i started with with if else condition and I am stuck in it since a week. I have read as many articles as I could but I am not understanding what is the problem here. I know this must a very basic for you guys but please help me :)
Here is the script that I wrote
#!/bin/sh
count=102
if [$count -gt 100]
then
echo "$count is greater than 100"
else
echo "$count is less than 100"
fi
When I execute this script, I am getting below error.
./count.sh: line 5: [102: command not found
102 is less than 100
*I also tried taking user input not just with integer but with string also, still getting the same error. please help :)
Thank you in advance
you need to provide space after [ and before ]. For e.g
[ $count -gt 100 ]
Sample code :
count=102
if [ $count -gt 100 ]
then
echo "$count is greater than 100"
else
echo "$count is less than 100"
fi

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

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 ...

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