The code-snippet below checks whether the instance_status available or not. If it is available, wait for 15 seconds and if and echo the instance status.
I am getting the following error:
line 38: While: command not found
code:
While ["$INSTANCE_STATUS" -eq "available"]
do
wait 15
if $INSTANCE_STATUS" -ne "available"
then
echo "$SOURCE_INSTANCE_ID" is "$INSTANCE_STATUS"
fi
done
Many simple mistakes in your original code: While not while, missing spaces around [, missing double-quotes, comparision operators -eq and -ne are not for comparing strings...
Beware using wait, which is a valid shell command. But probably you meant sleep instead. Here details about the differences.
Try this:
while [ "$INSTANCE_STATUS" = "available" ]
do
sleep 15 # 'waits' 15 seconds.
# ATTENTION: here you might need to 'refresh' $INSTANCE_STATUS value to avoid an endless loop.
# Solution suggested by #Charles Duffy:
# INSTANCE_STATUS=$(systemctl status "$SOURCE_INSTANCE_ID")
if [ "$INSTANCE_STATUS" != "available" ]
then
echo "$SOURCE_INSTANCE_ID is $INSTANCE_STATUS"
fi
done
Edited: many fixes to original answer suggested ...forced ;) by #Charles Duffy. Thanks Charles, now I realize my shell script skills are getting pretty rusty.
Related
I found a nifty little shell script that I wanted to use from this website here. I have followed everything step-by-step, but receive the following error when running this on my CentOS box.
./deploy: line 3: =: command not found
Line 3 only contains...
$ERRORSTRING = "Error. Please make sure you've indicated correct parameters"
I've tried toying around with a bit, but don't understand why it won't accept the "=" character. Is there something wrong with the script, or is it merely something different in the way that my server processes the script?
Thanks!
Gah, that script is full of bad scripting practices (in addition to the outright error you're running into). Here's the outright error:
$ERRORSTRING = "Error. Please make sure you've indicated correct parameters"
As devnull pointed out, this should be:
ERRORSTRING="Error. Please make sure you've indicated correct parameters"
A couple of lines down (and again near the end), we have:
echo $ERRORSTRING;
...which works, but contains two bad ideas: a variable reference without double-quotes around it (which will sometimes be parsed in unexpected ways), and a semicolon at the end of the line (which is a sign that someone is trying to write C or Java or something in a shell script). Use this instead:
echo "$ERRORSTRING"
The next line is:
elif [ $1 == "live" ]
...which might work, depending on whether the value of $1 has spaces, or is defined-but-blank, or anything like that (again, use double-quotes to prevent misparsing!). Also, the == comparison operator is nonstandard -- it'll work, because bash supports it in its [ ... ] builtin syntax, but if you're counting on having bash extensions available, why not use the much cleaner [[ ... ]] replacement? Any of these would be a better replacement for that line:
elif [ "$1" = "live" ]
elif [[ $1 == "live" ]]
elif [[ "$1" == "live" ]]
Personally, I prefer the last. The double-quotes aren't needed in this particular case, but IMO it's safest to just double-quote all variable references unless there's a specific reason not to. A bit further down, there's a elif [ $2 == "go" ] that the same comments apply to.
BTW, there's a good sanity-checking tool for shell scripts at www.shellcheck.net. It's not quite as picky as I am (e.g. it doesn't flag semicolons at the ends of lines), but it pointed out all of the actual errors in this script...
"Devnulls" answer was correct -- I had to remove the spaces around the "=" and remove the "$" from that line as well. The end result was...
ERRORSTRING="Error. Please make sure you've indicated correct parameters"
I've upvoted Devnull and gniourf_gniourf's comments.
Thank you to all whom have assisted!
I've poured over question after question searching here and on Google, but something in my syntax is messed up. Any assistance is appreciated.
Basically I've got another function that sets the load and threshold, as well as $FORCE variable. The script needs to do the "stuff" if it meets either the first or the second condition. The variables have been set correctly, which I've confirmed via echo in the script while debugging.
if ([ $LOAD -ge "$THRESH" ] || [ $FORCE=1 ]);
then
# do some other stuff
fi
From what I can see, my spacing of the brackets around the conditions are correct. They no longer produce bash [: missing]'` errors like they were.
The script runs fine, except for one issue...it runs no matter what. It's like it's completely ignoring the [ $FORCE=1 ] part, even though I can see that $FORCE is in fact actually 0.
Should I be using single quotes or some other method?
Try putting spaces around =:
if ([ $LOAD -ge "$THRESH" ] || [ $FORCE = 1 ]);
then
# do some other stuff
fi
How do I fix this error? I can't see anything wrong with my syntax.
ipcheck() {
echolog "[INFO] Enabling IP Forwarding..."
sudo echo 1 > /proc/sys/net/ipv4/ip_forward
if[$(cat /proc/sys/net/ipv4/ip_forward) == "0"]
then
echolog "[CRITICAL] Could not enable IP Forwarding!"
exit 0
fi
echolog "[INFO] IP Forwarding successfully enabled!"
}
I know this is a very basic script, but it's part of a bigger one. The error happens on the then statement.
Shell scripting tends to be a lot more whitespace sensitive than you might be used to if you've come from other programming languages (read: C). Your if line has the problems. You are probably looking for:
if [ $(cat /proc/sys/net/ipv4/ip_forward) == "0" ]
The thing to remember here is that [ is not part of any special if syntax - it's the name of a program (sometimes a shell builtin). If you think of it like that, you can see how the command line parser needs it to be separated. Similarly, the [ command (or builtin) expects the closing ] to be separated from its other arguments, so you need a space before it, too.
The problem is that you need a space between if and [. The lack of a space is confusing bash's parser.
Place a space between the if and [$(cat...] section on line 4. For this script to run, you'll also need a space on the ] on the same line.
On a related note, if you're not using indentation in your shell scripts, you should seriously consider it as it makes maintenance and legibility of your code much easier.
Slightly refactored (improved) version that is not bash dependant:
#!/bin/sh
ipcheck() {
echolog "[INFO] Enabling IP Forwarding..."
sudo echo 1 > /proc/sys/net/ipv4/ip_forward || {
echolog "[CRITICAL] Error echoing to ip_forward file"
exit 1
}
# Paranoia check :)
status=$(cat /proc/sys/net/ipv4/ip_forward)
[ "$status" = "1" ] || {
echolog "[CRITICAL] Could not enable IP Forwarding!"
exit 1 # 1 is error in scripts, 0 is success
}
echolog "[INFO] IP Forwarding successfully enabled!"
}
Preferably make an error() function, perhaps something like:
# Call it like: error "[INFO] Could not ..."
error() {
echolog "$*"
exit 1
}
Oh and yeah, as everyone else pointed out, don't forget the spaces :)
So I'm pretty new to bash scripting but so far tldp.org has been a good friend. Anyways I've confused myself and swearing to much so looking for help in clarification: I declare a variable like such
MAXseeds=-1;
sumS=0
I do a bunch of things in my script and get a new value for sumS which is an integer value. I would then like to compare MAXseeds and sumS if sumS is larger make MAXseeds equal to sumS. I do this by:
echo $MAXseeds
echo $sumS
if [ $MAXseeds -lt $sumS ];
then
MAXseeds = $sumS
best_file=$COUNT
fi
echo $MAXseeds
This from what I can tell should work however the terminal output I get when running over this section of script is
-1
492
lookup.sh: line 34: MAXseeds: command not found
-1
Basically I am wondering what I am doing wrong here? why does it respond with command not found? Any explanation to why this is incorrect would be greatly appreciated.
Try this:
if [ $MAXseeds -lt $sumS ];
then
MAXseeds=$sumS
best_file=$COUNT
fi
Without the spaces around "=".
If you put a space after "MAXseeds", then it will be interpreted as a command. And of course, it is not a command, thus you get your error message.
This question already has answers here:
How can I declare and use Boolean variables in a shell script?
(25 answers)
Checking the success of a command in a bash `if [ .. ]` statement
(1 answer)
Closed 5 years ago.
I have defined the following variable:
myVar=true
now I'd like to run something along the lines of this:
if [ myVar ]
then
echo "true"
else
echo "false"
fi
The above code does work, but if I try to set
myVar=false
it will still output true.
What might be the problem?
edit: I know I can do something of the form
if [ "$myVar" = "true" ]; then ...
but it is kinda awkward.
Thanks
bash doesn't know boolean variables, nor does test (which is what gets called when you use [).
A solution would be:
if $myVar ; then ... ; fi
because true and false are commands that return 0 or 1 respectively which is what if expects.
Note that the values are "swapped". The command after if must return 0 on success while 0 means "false" in most programming languages.
SECURITY WARNING: This works because BASH expands the variable, then tries to execute the result as a command! Make sure the variable can't contain malicious code like rm -rf /
Note that the if $myVar; then ... ;fi construct has a security problem you might want to avoid with
case $myvar in
(true) echo "is true";;
(false) echo "is false";;
(rm -rf*) echo "I just dodged a bullet";;
esac
You might also want to rethink why if [ "$myvar" = "true" ] appears awkward to you. It's a shell string comparison that beats possibly forking a process just to obtain an exit status. A fork is a heavy and expensive operation, while a string comparison is dead cheap. Think a few CPU cycles versus several thousand. My case solution is also handled without forks.