I am new to Shell script , I am trying to run a simple code
Input for the test.sh file will be input1 and input2 (For Example ./test_script.sh input1 input2)
#!/bin/bash
export a=$1
export b=$2
if ["$a" -eq ''] && ["$b" -eq '']
then
echo "Please enter an input"
elif ["$a"] && ["$b" -eq '']
then
echo "the value of a is $a"
elif ["$a"] && ["$b"]
then
echo "The value of a is $a and b is $b"
else
print "Something went wrong "
fi
Is this syntax correct because I am getting an error , Need help to find where I am going wrong
./test.sh 10 11
./test.sh: line 5: [10: command not found
./test.sh: line 10: [10]: command not found
./test.sh: line 14: [10]: command not found
./test.sh: line 19: print: command not found
Related
how to execute if statement line by line in bash??
for i in $(cat $install_script)
do
if [ "$i" = "$check" ]; then
count=$((count+1))
echo whiptail --title "Install SmartBoard" --yesno "${scripts_name[$count]}" 7 $((${#scripts_name[$count]} + 5))
if (( $? == 0 )); then
skip=false
else
skip=true
fi
fi
if [ "$i" == "$check wifi" ]; then
break
fi
if [ $skip == true ]; then
continue
else
sh -c $i ## process line by line
fi
done
error !!
sh: 1: Syntax error: end of file unexpected (expecting "then")
sh: 1: Syntax error: "then" unexpected
sh: 1: Syntax error: "else" unexpected
sh: 1: Syntax error: "fi" unexpected
i'm not good english so if my posts word be rude Sorry that. :(
I wrote a little Bash script called cs210_list.sh, and it is giving me the following errors when i run it either as bash cs210_list.sh or as
sh cs210_list.sh in linux:
cs210_list.sh: line 2: [0: command not found
cs210_list.sh: line 21: syntax error near unexpected token `fi'
cs210_list.sh: line 21: ` fi'
this is my very first bash script ever. I can't figure why I'm getting the errors. BTW, "(line 21)" is not actually in my code, it's just there for your reference.
$? -eq 0 This command is supposed to determine if there as a argument
#!/bin/bash
if [$? -eq 0];
then
echo "Error: Missing argument";
exit 1
fi
if [-e "$1"];
then
if [-d "$1"];
then
for i in $(ls)
do
if [-d "$i"];
then
echo "File: $i"
echo ""
else
echo "File: $i"
echo " Size: " $(stat -c%s $i)
fi
fi (line 21)
else
echo "$Error: File doesn't exist"
fi
done
I am trying to write a simple bash script with a for-loop inside an if-else statement. The following code works:
if [ $# -le 0 ]; then
echo 'No arguments provided.'
else
echo "Arguments detected."
fi
But as soon as I replace the second echo with a for-loop, it breaks:
if [ $# -le 0 ]; then
echo 'No arguments provided.'
else
for i in "$#"
do
echo "Arguments detected: $i"
done
fi
I've tried to execute only the for-loop and I'm given an error, so I think the for-loop is wrong somehow, but I don't know how. The error is:
bash: C:/Users/Angleton/.bashrc: line 45: syntax error near unexpected token `fi'
bash: C:/Users/Angleton/.bashrc: line 45: ` fi'
This question already has an answer here:
Bash scripting unexpected operator
(1 answer)
Closed 6 years ago.
What is wrong in my code?
#!/bin/sh
LOOK_FOR="$1"
for i in `find $2 -name "*jar"`; do
echo "Looking in $i ..."
#jar tvf $i | grep $LOOK_FOR > /dev/null
jar tvf "$i" | grep "$LOOK_FOR"
if [ $? == 0 ] ; then
echo "==> Found \"$LOOK_FOR\" in $i"
fi
done #line 13
Output
wk#wk-laptop:$ sh lookjar.sh org/apache/axis/message/addressing/EndpointReference /media/0C06E20B06E1F61C/uengine/uengine
Looking in /media/0C06E20B06E1F61C/uengine/uengine/defaultcompany/build/uengine_settings.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/defaultcompany/WebContent/uengine-web/lib/FCKeditor/WEB-INF/lib/commons-fileupload.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/defaultcompany/WebContent/uengine-web/lib/FCKeditor/WEB-INF/lib/FCKeditor-2.3.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/defaultcompany/WebContent/uengine-web/processmanager/signedmetaworks.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/hsqldb/lib/hsqldb.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/hsqldb/lib/servlet.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/src/lib/commons-discovery.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/src/lib/google.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/src/lib/jxl.jar ...
You need to use = instead of == in the [ $? == 0 ] line.
You should change that to:
if [ $? -eq 0 ]; then
...
-eq does a numeric comparison.
You can also take advantage of the fact that in shell a return value of 0 is considered success and write your code like this:
if jar tvf "$i" | grep "$LOOK_FOR"; then
...
#!/bin/sh
LOOK_FOR="$1"
find $2 -name "*jar"`| while read -r file
echo "Looking in $file ..."
jar tvf "$file" | grep "$LOOK_FOR"
if [ $? -eq 0 ] ; then
echo "==> Found \"$LOOK_FOR\" in $file"
fi
done
Try:
if [[ $? == 0 ]]; then
echo "==> Found \"$LOOK_FOR\" in $i"
fi
I have this simple bash script that keeps failing with the following messages:
./servo.sh: line 7: syntax error near unexpected token then'
./servo.sh: line 7: if[ "$level" -eq 1 ]; then'
And my bash script:
#!/bin/bash
level=1
while :
do
if[ $level -eq 1 ]; then
echo "hello"
else
echo "else"
fi
done
What am I doing wrong?
add a space between the if and the [