I have a secondary list of start up programs that I would like to start in case I am working. So I have added this shell script to the start up of my system running on Ubuntu.
echo "Do you want to start the start up applications[Y/n]?"
while read inputline
do
what="$inputline"
break
done
if [ "$what" == "Y" -o "$what" == "y" ]
then
. ~/bin/webstorm.sh &
workrave &
firefox &
. ~/bin/AptanaStudio3
fi
I keep getting this error that says something like [: Y: unexpected operator all the time and never starts the programs.
Disclaimer: I have no idea how to write shell scripts.
Try changing:
if [ "$what" == "Y" -o "$what" == "y" ]
to
if [[ "$what" == "Y" ]] || [[ "$what" == "y" ]]
What is your shebang line? #!/bin/bash or #!/bin/sh?
If you are intending to use bash as script interpreter, don't forget shebang line.
(Your code is working with bash on my system at least..)
Related
I am a newbee in shell scripting and am learning it from online tutorial website .
In a particular code I came across the following line :
[ -z "${starkisland}" ] && { echo "`basename $0`: ERROR: starkisland environment variable not defined !" ; exit 2 ; }
Here I see [ -z "${starkisland}" ] is an if condition meaning if the file starkisland is of zero size and the part after && is like the part that is to be executed if the condition is true. But I guess && is the symbol for AND condition, if I am not wrong.
Can anyone help me in understanding the statement and logic behind writing like this if && is the AND condition.?
You should read this
if [ CONDITION ] && {ACTION1} || {ACTION2}
as
If CONDITION then ACTION1 else ACTION2
In fact these are not logical operators, these flow control operators
&& means "execute if exit code is 0"
|| means "execute if exit code is not 0"
Usually I add "&" character to start my process in backgroud, exemple :
user#pc:~$ my_script &
But how can I make it in background without "&" character ?
#!/bin/bash
#What can I add here to hide current process ($$) and to release focus ?
start_server()
{
#my script here with infinite loop ...
}
Thanks guys.
#!/bin/bash
if [[ "$1" != "--nodaemon" ]]; then
( "$0" --nodaemon "$#" </dev/null &>/dev/null & )
else
shift
fi
#...rest of script
What this does is check to see if its first argument is "--nodaemon", and if so fire itself ("$0") off in the background with the argument "--nodaemon", which'll prevent it from trying to re-background itself in a sort of infinite loop.
Note that putting this as the first thing in the script will make it always run itself in the background. If it only needs to drop into the background under certain conditions (e.g. when run with the argument "start"), you'd have to adjust this accordingly. Maybe something like this:
#!/bin/bash
start_server()
{
#my script here with infinite loop ...
}
if [[ "$1" = "start" ]]; then
( "$0" start-nodaemon </dev/null &>/dev/null & )
elif [[ "$1" = "start-nodaemon" ]]; then
start_server
elif #.....
So in my bash script, I output status report to terminal as well as write it to the log file. I wanted to use a bash ternary operator that will output to terminal as well as write a log file if variable LOG_TO_TERMINAL is true, and if that is set to false, just write to a log file without outputting status to the terminal.
My sample code looks like this:
[[ $LOG_TO_TERMINAL ]] && echo "error message" >> $LOG_FILE || echo "error message" | tee -a $LOG_FILE
which just logs the file instead of echoing to the terminal no matter whether I set LOG_TO_TERMINAL to true or false.
To isolate the problem, I tried simplifying the code to:
[[ $LOG_TO_TERMINAL ]] && echo "log to terminal" || echo "don't log to terminal"
But this code snippet also echoes "log to terminal" no matter what its value is.
The test [[ $LOG_TO_TERMINAL ]] tests whether LOG_TO_TERMINAL has a value or not. Nothing else. The shell doesn't treat false (or 0 or null etc.) as special false-y values.
If you want some other test you need to test specifically for that.
[[ $LOG_TO_TERMINAL = true ]]
or
[[ $LOG_TO_TERMINAL != false ]]
or
[[ $LOG_TO_TERMINAL = 1 ]]
etc.
If you were expecting to use the return code from the true and/or false commands then you need $LOG_TO_TERMINAL && Y || Z or similar to run the command stored in the variable (though I wouldn't recommend this version of this test).
Also note that X && Y || Z is not a ternary operation in the shell. See the Shellcheck wiki for warning SC2015 for more about this.
You want this:
[[ $LOG_TO_TERMINAL = 1 ]] && echo "log to terminal" || echo "don't log to terminal"
I want to make this script repeat the somescript.sh when I click return until I write q. I think I've gotten pretty close, but I can't make it set $actionLoop to 0 if it reads "q" what am I doing wrong here?
#!/bin/bash
$actionLoop = 1
while [ ${actionLoop} 1 ]
do
echo "do another random review script?"
sh /somescript.sh
echo "Done. Press q to quit."
read response
[ $response = "q" ] && $actionLoop = 0
done
Change
$actionLoop = 1
to
actionLoop=1
Similarly, this line
[ $response = "q" ] && $actionLoop = 0
to
[ $response = "q" ] && actionLoop=0
You can't use $ when assigning a variable and you can't have whitespaces around the assignment either.
IMO, you don't need that variable at all. Use an infinite loop and break it when q is given.
while : ;
do
echo "do another random review script?"
sh /somescript.sh
echo "Done. Press q to quit."
read response
[[ "$response" = "q" ]] && break
done
I personally prefer bash built-in [[ ]] instead of [ (test) command. Some prefer [ for compatibility with older shells.
I have written this script as a module in installing postgres and this is to show the user that the database has been created and get his/her input as to they see the database was created. When I run the script, I get the error
./dbtest: line 39: [: missing `]'
I have tried adding " " around yes and '' around yes and I can't figure out what is missing. The script is as follows
#
#
# Check to make sure the database was created and who is the owner
#
#
if [ -f showdb ];
then
cp showdb /home
else
echo " show database file is missing "
fi
if [ -f /home/showdb ];
then
su - postgres -c '/home/showdb'
echo " Do you see the data name created listed above? "
echo " "
echo " Type yes or no (type out the whole word please) "
echo " "
read dbawr
if [ $dbawr == yes && $dbawr == Yes ];
then
echo "Great!"
exit
else
echo " Please contact tech support "
pause " Press [CTRL] [Z] to exit the program now "
fi
else
echo " File Missing!!"
fi
What am I missing in this script?
Thanks!
You can't use the boolean && operator withing the single brackets conditional [ test = test ]. If you're using bash (or a similar shell), the preferred syntax is to use the double brackets:
[[ this == this && that == that ]]
If you're worried about portability, then you should stick with the single brackets, but use them like so:
[ this = this ] && [ that = that ]
Note that I didn't use the double equals (==). That's not posix compliant either.