I have written a simple shell script to do some automation work. Basically the script searches for all the files in the current path and if the file is a specified one, it does some action.
Below are the relevant lines —
#!/bin/bash
for i in `ls *`
do
if [$i =="ls.sh"]
then .... //do something
fi
done
However, the string comparision in line 3 is not working and I am getting this when I run the script —
./ls.sh: line 3: [scripth.sh: command not found
./ls.sh: line 3: [scripth.sh~: command not found
./ls.sh: line 3: [test.sh: command not found
What is the correction to be done ?
There are several problems.
In line 1, you are not doing what you think you are. You should put a backquote around ls *:
That will go through all files that list in the current directory. Your line will not run any command, but instead it will use * to get all files and your list will include a word “ls” at the front.
try this from a command line:
You might just want to do:
Second problem. Put spaces inside your square brackets:
The spaces are necessary.
Third problem. Use one = for string comparison