I have the following code and I get the Error "line 9: [: not found":
#!/bin/sh
msg=$(dmesg | tail -n1)
echo "$msg"
if [ "$msg" = "Tasklet grp12" ]
then
echo "Test was successful, Strings are equal."
else
echo "Test failed, Strings are not equal."
fi
thanks to Charles Duffy! I had to check the box "Builtin version of 'test'" in the shell section of the make menuconfig menu of busybox, to enable string comparison. Now the code works.
Since I had grep activated I first tried another solution which also worked but has a bad performance for sure:
#!/bin/sh
msg=$(dmesg | tail -n1)
echo "$msg"
tasklet="Tasklet grp12"
if ( echo "$msg" | grep "^$tasklet$" )
then
echo "Test was successful, Strings are equal."
else
echo "Test failed, Strings are not equal."
fi
Related
I am writing a script in bash, and I have a problem with it:
select opt in "${options[#]}"
do
case $opt in
"1) Check Cassandra Status.")
ssh skyusr#"$IP" "export JAVA_HOME=/opt/mesosphere && /var/lib/mesos/slave/slaves/*/frameworks/*/executors/*/runs/latest/apache-cassandra-3.0.10/bin/nodetool -p 7199 status" | sed -n '6,10p' | awk '{print $1,$2}' | DN="$(grep DN)" | [[if [[!DN]]; echo "All Good" else "Node(s) "$DN" is Down" ;fi]]
;;
"2) Run Repair on all nodes.")
echo "you chose choice 2"
;;
"3) Run Refresh on specific keyspace/Table.")
echo "you chose choice 3"
;;
"4) Optional")
echo "This option disabled"
;;
"Quit")
break
;;
*) echo invalid option;;
esac
done
It gives me
error:line 16: [[if: command not found
All was working until I added this if command, I need to echo a message if $DN is empty else echo another message .
You seem to be confused about some of Bash's basic concepts like pipelines (|) versus compound commands (if and [[). For example awk '{print $1,$2}' | DN="$(grep DN)" does probably not do what you expect:
$ echo $'a\nb'
a
b
$ echo $'a\nb' | B=$(grep a)
$ echo $B
Note how the variable B is not set to "a".
Furthermore your syntax DN="$(grep DN)" | [[if [[!DN]]; echo "All Good" is complete nonsense. You best start by reading some introduction. Then you can continue along the lines of:
A=$(....)
[[ -z $A ]] && echo "A is empty"
# or
if [[ -z $A ]] then echo "A is empty"; else echo "A is not empty"; fi
Change your if condition like below. Your if condition syntax is not correct and then part is also missing. I have just corrected the if condition rest you need to check again.
if [[ DN == "" ]]; then echo "All Good" else echo "Node(s) $DN is Down" ; fi
Is it possible to set a variable from the output of a command inside of a conditional where the conditional is false if nothing gets assigned to the variable.
If I set the variable to a grep with no return and then test:
test=$(echo hello | grep 'helo')
if [[ ! -z $test ]]; then
echo "is set"
else
echo "not set"
fi
Output: not set (this is expected)
But I'm trying to put it all into one statement like this:
test=
if [[ ! -z test=$(echo hello | grep 'helo') ]]; then
echo "is set"
else
echo "not set"
fi
output: "is set" (expected not set)
grep returns success if there is a match, so you can just do:
if test=$(echo hello | grep 'helo')
then
echo "Match: $test"
else
echo "No match"
fi
If you're running something that doesn't differentiate by exit code, you can assign and check in two statements on the same line:
if var=$(cat) && [[ -n $var ]]
then
echo "You successfully piped in some data."
else
echo "Error or eof without data on stdin."
fi
(or ; instead of && if you want to inspect the result even when the command reports failure)
Bit of a hack, using the shell's parameter expansion alternate value syntax, echo -e and some backspaces:
test=$(echo hello | grep 'helo'); echo -e not${test:+\\b\\b\\bis} set
Which outputs is set or not set depending on what grep finds.
I am running a script from command line that runs a bunch of Jmeter tests if 4 variables are set.
The script works but I have added parts so the script will end if the server is unknown.
if [ echo "$2" != | grep -iq "^hibagon" ] || [ echo "$2" != | grep -iq "^kameosa" ] ;then
echo "Unkown server stopping tests"
else
echo "Continueing to tests"
when this section of the script runs it will end the script if hibagon or kameosa is not found (not case sensitive).
I want the command line to echo Unknown server stopping tests then end but at the moment it just ends with no echo
That's a strange syntax. Try this:
if echo "$2" | grep -iq "^hibagon\|^kameosa";
then
echo "Continuing to tests"
else
echo "Unkown server, stopping tests"
fi
Or if you're using bash:
if [[ "$2" =~ ^hibagon ]] || [[ "$2" =~ ^kameosa ]]
then
echo "Continuing to tests"
else
echo "Unkown server, stopping tests"
fi
First the test [ echo "$2" != | grep -iq "^hibagon" ] is wrong, then you can use only one (extended)grep with the negation flag -v putting together the two words in one regex ^(hibagon|kameosa). Also the fi was missing. but I suppose it's only a typo here.
if echo "$2" | egrep -ivq "^(hibagon|kameosa)"; then
echo "Unknown server stopping tests"
else
echo "Continuing to tests"
fi
If you like it, even:
if egrep -ivq "^(hibagon|kameosa)" <<< "$2"; then
Hello I have the following script:
#! /bin/bash
Output=$(defaults read com.apple.systemuiserver menuExtras | grep Bluetooth.menu)
Check="\"/System/Library/CoreServices/Menu Extras/Bluetooth.menu\","
echo $Output
echo $Check
if [ "$Output" = "$Check" ]
then
echo "OK"
else
echo "FALSE"
echo "Security Compliance Setting 'Show Bluetooth Status in Menu Bar(2.1.3)' has been changed!" | logger
fi
When you run it, both variables have the exact same output however the check always says it is FALSE
here is the out put from my terminal:
"/System/Library/CoreServices/Menu Extras/Bluetooth.menu",
"/System/Library/CoreServices/Menu Extras/Bluetooth.menu",
FALSE
Any idea why it is not detecting that they are the same?
As everyone in the comments suspected, the problem is whitespace in $Output (which echo $Output removes); specifically, 4 leading spaces (note that in the following, "$ " is my shell prompt):
$ defaults read com.apple.systemuiserver menuExtras | grep Bluetooth.menu
"/System/Library/CoreServices/Menu Extras/Bluetooth.menu",
$ Output=$(defaults read com.apple.systemuiserver menuExtras | grep Bluetooth.menu)
$ echo $Output
"/System/Library/CoreServices/Menu Extras/Bluetooth.menu",
$ echo "[$Output]"
[ "/System/Library/CoreServices/Menu Extras/Bluetooth.menu",]
$ Check=" \"/System/Library/CoreServices/Menu Extras/Bluetooth.menu\","
$ if [ "$Output" = "$Check" ]; then echo "OK"; else echo "FALSE"; fi
OK
Note that since the number of spaces might not always be the same, it might be safer to use bash's wildcard matching capability within a [[ ]] conditional expression (this will not work with [ ]):
$ Check="\"/System/Library/CoreServices/Menu Extras/Bluetooth.menu\","
$ if [[ "$Output" = *"$Check" ]]; then echo "OK"; else echo "FALSE"; fi
OK
You could also skip the string comparison entirely, and just use the fact that grep returns a success status only if it finds a match:
#!/bin/bash
if defaults read com.apple.systemuiserver menuExtras | grep -q "/System/Library/CoreServices/Menu Extras/Bluetooth.menu"; then
echo "OK"
else
echo "FALSE"
echo "Security Compliance Setting 'Show Bluetooth Status in Menu Bar(2.1.3)' has been changed!" | logger
fi
I have created a simple BASH script that checks every hour for the presence of a file on a remote server. It worked error-free until I was asked to move it to a server that runs KSH.
The portion of code that errors-out is this one:
connect_string=$UID#$SERVER:$srcdir/$EVENTFILE
result=`sftp -b "$connect_string" 2>&1`
if [ echo "$result" | grep "not found" ]; then
echo "not found"
else
echo "found"
fi
These are the errors it throws:
-ksh: .[51]: [: ']' missing
grep: ]: No such file or directory
found
It still runs though and confirms that the file I am polling for is there but I need to fix this. I changed the if statement like so
if [[ echo "$result" | grep "not found" ]]; then
but it fails right away with this error
-ksh: .: syntax error: `"$result"' unexpected
What am I missing?
Your basic syntax assumptions for if are incorrect. The old [...] syntax, calls the test builtin, and [[...]] is for textual pattern matching.
As #shelter's comment, the correct syntax is:
connect_string="$UID#$SERVER:$srcdir/$EVENTFILE"
result=`sftp -b "$connect_string" 2>&1`
if echo "$result" | grep "not found" ; then
echo "not found"
else
echo "found"
fi
But this is an unnecessary use of the external grep program, you can use shell text comparison:
if [[ $result == *not\ found* ]] ; then
echo "not found"
else
echo "found"
fi
(tested with bash and ksh)
Your solution:
EXIT=`echo $?`
if [ $EXIT != 0 ]
then
...
fi
Can be improved. First, if you are going to do an arithmetic comparison, then use ((...)), not test, and I can't figure out why you have the EXIT variable:
if (( $? != 0 ))
then
...
fi
But to go full circle, you actually only need:
if sftp -b "$connect_string" 2>&1
then
...
fi
echo "$result" | grep "not found"
#capture exit status code from previous command ie grep.
if [[ $? == 0 ]]
than
echo "not found"
else
echo "found"
fi
It appears you're struggling with a basic tenet of bash/ksh control structures.
Between the if and the then keywords, the shell expects one or more commands, with
the last command in the series deciding how the if statement is processed.
The square brackets are only needed if you actually need to perform a comparison. Internally they are equivalent to the test command - if the comparison succeeds, it
results in an exit status of 0.
Example:
$ [ a == a ]
$ echo $?
0
$ [ a == b ]
$ echo $?
1
Which is equivalent to:
$ test a == a
$ echo $?
0
$ test a == b
$ echo $?
1
I changed my approach to this.
connect_string=$UID#$SERVER:$srcdir/$EVENTFILE
result=`sftp "$connect_string" 2>&1`
EXIT=`echo $?`
if [ $EXIT != 0 ]
then
echo "file not found"
exit 1
else
echo "file found"
exit 0
fi
It takes care of my problem. Thanks to all.