How to fix this bash script? - bash

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

Related

Ignore exit code in bash

I'm testing about exit codes in bash and I coded the following script:
read -p "Path: " path
dr $path 2> /dev/null
echo "Command output level: "$?
if [ $? = 0 ]
then
echo "Command success"
elif [ $? = 127 ]
then
echo "Command not found"
else
echo "Command failed or not found"
fi
Now, I've been doing some research and I want to know if there's a way to make the very last "echo" avoid changing the exit code, if there's any I haven't found it.
I understand that the exit code is changed from 127 (yes, dr is on purpose to provoke the exit code) to 0 when I executed it.
Every command sets $?. If you wish to preserve the exit status of a specific command, you must save the value immediately.
dr $path 2> /dev/null
dr_status=$?
echo "Command output level: $dr_status"
if [ $dr_status = 0 ]
then
echo "Command success"
elif [ $dr_status = 127 ]
then
echo "Command not found"
else
echo "Command failed or not found"
fi
However, you can sometimes avoid repeated commands that would reset $?. For example,
dr $path 2> /dev/null
case $? in
0) echo "Command success" ;;
127) echo "Command not found" ;;
*) echo "Command failed or not found" ;;
esac

Unexpected end of file while trying if else condition

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

"Too many arguments" error in shell script

Here's my script:
if [ awk '$0 ~ /Failed/ { print }' $(pwd)/unity.log ]; then
echo "Build Failed"
exit 1
else
echo "Build Success"
exit 0
fi
The gist is that I am checking the file for "Build Failed" message and exiting 1 if failed.
If what I understand is correct, awk will return blank string if there is no text in the file and some text if it's found. But it shoots syntax error : ./Scripts/build.sh: line 41: [: too many arguments
Remove the [], use grep:
if grep -qw Failed unity.log; then
echo "Build Failed"
exit 1
else
echo "Build Success"
exit 0
fi
You could take advantage of the default exit value being 0. Also, there's no reason to do $(pwd)/filename; just do filename.
grep -qw Failed unity.log || { echo "Build Failed"; exit 1; }
echo "Build Success"
It would be also more idiomatic to send the error message on failure to stderr (with echo >&2).
try this:
if [ ! "$(grep 'Build Failed' unity.log)" ]; then
exit 1
fi

While loop in BASH script causing syntax error

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

Getting an EOF error, I've searched for sytax errors cannot see

Getting these two errors when running a simple script to just make a repo for a user.
This is a bash script
ERROR:
./createMyRepo.sh: line 48: unexpected EOF while looking for matching `"'
./createMyRepo.sh: line 52: syntax error: unexpected end of file
#!/bin/bash
# This script is used to automate the repo
if [ -z `$1` ]
then
echo "No user was input, please input a user and try again"
exit
else
cd /home/$1
if [ $? -eq 0 ]
then
echo "Successfully changed directory to user's home"
else
echo "Failed to cd directory, trying to create directory now."
mkdir /home/$1
if [ $? -eq 0 ]
then
echo "Successfully created the directory location
else
echo "Failed to create directory, exiting."
exit
fi
fi
mkdir project.git
if [ $? -eq 0 ]
then
echo "Succesfully created project.git directory"
else
echo "Failed to create project.git directory attempting to see if the directory already exists"
cd project.git
if [ $? -eq 0 ]
then
echo "Successfully changed to this directory"
else
echo "This directory cannot be created and does not exist. exiting..."
exit
fi
fi
cd project.git
echo "creating git repo"
git --bare init
if [ $? -eq 0 ]
then
echo "DONE Created repo"
else
echo "FAIL repo did not create"
fi
fi
oops ... look like a typo error
echo "Successfully created the directory location
should be in this form:
echo "Successfully created the directory location"
The line echo "Successfully created the directory location is missing a " at the end, so that Bash is completely confused about where your strings are and aren't.
(Hat-tip to Stack Exchange's syntax highlighting, which makes the problem obvious!)
Also, I suggest you adopt a better indentation scheme; your current scheme makes it very difficult to trace nested ifs and so on.

Resources