I am just starting to learn shell and am getting this error team.sh: line 9: syntax error: unexpected end of file when trying conditional if else:-
#!/bin/sh
result=2
tmp=2
if [ $result == $tmp ]
echo "App is running"
else
echo "App is down"
fi
There are multiple ways to use conditional statements in shell. few of them are :-
Method 1:-
#!/bin/sh
result=2
tmp=2
if [ $result == $tmp ]
then
echo "App is running"
else
echo "App is down"
fi
method 2:-
#!/bin/sh
result=2
tmp=2
if [ $result == $tmp ] ; then
echo "App is running"
else
echo "App is down"
fi
this link can be useful
Related
I want to loop through all the files in the directory and check their perms (if user wants) but for some reason I'm getting this error:
./perms.sh: line 12: syntax error near unexpected token `;;'
./perms.sh: line 12: ` r) if [ -r $i ] then echo 'True' else echo 'False' fi ;;'
here is my code:
#!/bin/bash
for i in *
do
echo "Do you want to check rights for $i (y/n)"
read marzi
if [ $marzi = 'y' ]
then
echo 'which commands to check? '
read check
case $check in
r) if [ -r $i ] then echo 'True' else echo 'False' fi ;;
w) if [ -w $i ] then echo 'True' else echo 'False' fi ;;
x) if [ -x $i ] then echo 'True' else echo 'False' fi ;;
*) echo 'unrecognized!' ;;
esac
else
echo "skipped $i"
fi
done
does this have something to do with apostrophe?
maybe correct inline if format should be
if [ -r $i ]; then echo 'True'; else echo 'False'; fi
make sure to make change for r and w and x
if [ "$var1" = "$var2" ]; then
if [ -f "$file1" ]; then
echo "file1 exists"
if [ -f "$file2" ]; then
echo "file2 exists"
if ["$incValue" == "1"]; then
break
fi
fi # (I am getting error here!)
else
echo "files are not present"
sleep 300
fi
else
echo "files are present"
fi
The above code does'nt works for me. I am getting error as : syntax error near unexpected token `fi'
I am not able to understand what is wrong in my code.
That extra fi where you see the error is not needed, and it breaks your if-else loop. You also need a closing fi at the end of your code.
if [ "$var1" = "$var2" ]; then
if [ -f "$file1" ]; then
echo "file1 exists"
if [ -f "$file2" ]; then
echo "file2 exists"
if ["$incValue" == "1"]; then
break
fi
else
echo "files are not present"
sleep 300
fi
else
echo "files are present"
fi
fi
You need a space after the [ and a space before the ] in the following line.
if [ "$incValue" == "1" ]; then
I have this script here:
killall -q $1
#turns out system already has a method for this, yey
#-q because we already have a error handling
returned=$?
#does this so that it doesn't read the success state of the ifs
if [ $returned = 0 ]
then
printf "Process kill attempt returned "
echo $returned
echo "Process killed sucessfully."
#yey we did it
else
#oh noes it failed
printf "Process kill attempt returned "
echo $returned
echo "Process kill attempt failed."
printf "Most likely cause of failure: "
if [ $returned = 1 ]
then
echo "process does not exist or is not running"
elif [ $returned = 2 ]
echo "process is system task; insufficient permissions"
else
echo "unknown failure " $returned "; no known fail in database has this value"
fi
fi
When looking at it, I see no problem, yet when run, I get this error.
nathan#HAL-LINUX:~$ xscreensaver
nathan#HAL-LINUX:~$ killproc xscreensaver
/usr/local/bin/killproc: line 23: syntax error near unexpected token `else'
/usr/local/bin/killproc: line 23: ` else'
nathan#HAL-LINUX:~$
My original script omitted the nested if, by just straight up telling you it failed with error 3 or 1 or 2.
Should I go back? Or is there a way to fix this?
You forgot the then after your elif:
if [ $returned = 1 ]
then
echo "process does not exist or is not running"
elif [ $returned = 2 ]
then
echo "process is system task; insufficient permissions"
else
echo "unknown failure " $returned "; no known fail in database has this value"
fi
So I have this little problem. I'm not sure where it went wrong because I'm pretty sure I got the code right.
Here's the code:
#!/bin/bash
playerHP=100
echo "Hello World"
echo "HP: $playerHP"
echo "Continue? (Y/N):"
read -p $confirm
if [ "$confirm" = "y" ]
then
echo "Yes"
elif [ "$confirm" = "n" ]
then
echo "No"
else
echo "No such command"
fi
Here's the result:
Unrelated: read needs a prompt after -p. Blend the previous echo into it, and while at it, remove the $ from the variable name there.
read -p "Continue? (Y/N):" confirm
The error message is confusing. Don't you have MSWin line ends in the script?
Hi I have modified your script below use it. Working fine for me
#!/bin/bash
playerHP=100
echo "Hello World"
echo "HP: $playerHP"
read -p "Continue? (Y/N): " confirm
echo $confirm
if [ "$confirm" = "y" ]
then
echo "Yes"
elif [ "$confirm" = "n" ]
then
echo "No"
else
echo "No such command"
fi
I recently started writing BASH scripts, and I am currently trying to practice using while loops. However, when I run the following block of code, the command prompt responds with:
run.command: line 12: syntax error near unexpected token `done'
run.command: `done'
Then the program shuts off.
This is the code I am running.
#!/bin/bash
echo -e "text"
c=false
while true; do
printf ">> "
i=read
if [$i = "exit"]; then
exit
else if [$i = "no"]; then
echo "no"
else
echo -e "Error: $i is undefined"
fi
done
I did some research on while loops, however my loop syntax seems correct. When I remove the done at the end, and Unexpected end of file error occurs. Any help would be appreciated!
You can use the -p option of read for the prompt and a case ... esac construction:
while true; do
read -r -p ">> " i
case "$i" in
"exit") exit 0 ;;
"no") echo "no" ;;
*) echo -e "Error: $i is undefined";;
esac
done
I fixed it myself!
#!/bin/bash
echo -e "text"
c=false
while true; do
printf ">> "
read i
if [ "$i" = "exit" ]; then
exit
elif [ "$i" = "no" ]; then
echo "no"
else
echo -e "Error: $i is undefined"
fi
done