What is the best way to make sure that all the environment variables I need for my script have been set?
Currently, I have multiple if-statements which is not very neat:
if [ -z "$VAR1" ]
then
echo VAR1 not set
exit
fi
if [ -z "$VAR2" ]
then
echo VAR2 not set
exit
fi
if [ -z "$VAR3" ]
then
echo VAR3 not set
exit
fi
Is there a better way?
You can use indirection:
vars="FOO BAR"
for var in $vars
do
[[ -z ${!var} ]] &&
echo "Variable ${var} is empty" ||
echo "The value of variable ${var} is ${!var}"
done
Use a for loop in (set of variables u want). Won't that work?
I have this function in my shell library:
# Check that a list of variables are set to non-null values.
# $#: list of names of environment variables. These cannot be variables local
# to the calling function, because this function cannot see them.
# Returns true if all the variables are non-null, false if any are null or unset
varsSet()
{
local var
for var ; do
eval "[ -n \"\$$var\" ] || return 1"
done
}
I use it in code like this:
varsSet VAR1 VAR2 VAR3 || <error handling here>
you can short them a lot:
[ -z "$FOO" ] && echo "FOO is empty"
[ -z "$BAR" ] && echo "BAR is empty"
a better way:
${FOO:?"FOO is null or not set"}
${BAR:?"BAR is null or not set"}
Of course if the number of variables you are going to test is not low, looping as suggested #Aviator maybe useful to avoid repeating code.
Upon #Aviator answer, I'd like to suggest to define a well commented variable containing a list of your variables-to-be-tested. This way you don't make your code cryptic.
TEST_FOR_IS_SET="FOO BAR BAZ"
Related
I write a function like this to .commands file, and I imported .commands in .zshrc
function testA() {
echo "start"
if [ "$1" == "a" ]; then
echo "ok"
fi
echo "end"
}
And when I run testA a in terminal
start
testA:2: = not found
What is the problem here?
Chapter 12 – "Conditional Expressions" of the zsh documentation states:
A conditional expression is used with the [[ compound command to test attributes of files and to compare strings.
This means, changing your conditional expression to use [[ ... ]] instead of [ ... ] should make it work:
function testA() {
echo "start"
if [[ "$1" == "a" ]]; then
echo "ok"
fi
echo "end"
}
You can simplify the problem to simply type a [a == a] or echo ==. This will also produce this error. The reason is that a = has specific meaning to zsh, unless it is followed by a white space.
You have three possible workarounds:
Either quote that parameter, i.e.
[ $1 "==" a ]
or use a single equal sign, i.e.
[ $1 = a ]
or use [[, which introduces a slightly different parsing context:
[[ $1 == a ]]
$ cat fun.sh
function test() {
echo "start"
if [ "a" == "a" ]; then
echo "ok"
fi
echo "end"
}
Source script file
$ source fun.sh
Output:
$ test
start
ok
end
Have two files:
file1 is having the key words - INFO ERROR
file2 is having the list of log files path - path1 path2
I need to exit out of the script if any of the condition in any of the loops failed.
Here is the Code:
#!/bin/bash
RC=0
while read line
do
echo "grepping from the file $line
if [ -f $line ]; then
while read key
do
echo "searching $key from the file $line
if [ condition ]; then
RC=0;
else
RC=1;
break;
fi
done < /apps/file1
else
RC=1;
break;
fi
done < apps/file2
exit $RC
Thank you!
The ansewer to your question is using break 2:
while true; do
sleep 1
echo "outer loop"
while true; do
echo "inner loop"
break 2
done
done
I never use this, it is terrible when you want to understand or modify the code.
Already better is using a boolean
found_master=
while [ -n "${found_master}" ]; do
sleep 1
echo "outer loop"
while true; do
echo "inner loop"
found_master=true
break
done
done
When you do not need the variable found_master it is an ugly additional variable.
You can use a function
inner_loop() {
local i=0;
while ((i++ < 5)); do
((random=$RANDOM%5))
echo "Inner $i: ${random}"
if [ ${random} -eq 0 ]; then
echo "Returning 0"
return 0
fi
done;
return 1;
}
j=0
while ((j++ < 5 )); do
echo "Out loop $j"
inner_loop
if [ $? -eq 0 ]; then
echo "inner look broken"
break
fi
done
But your original problem can be handles without two while loops.
You can use grep -E "INFO|ERROR" file2 or combining the keywords. When the keywords are on different lines in file1, you can use grep -f file1 file2.
Replace condition with $(grep -c ${key} ${line}) -gt 0 like this:
echo "searching $key from the file $line
if [ $(grep -c ${key} ${line}) -eq 0 ]; then
It will count the each key-word in your log-file. If count=0 (pattern didn't found), running then. If found at least 1 key, running else, RC=1 and exit from loop.
And be sure, that your key-words can't be substrings of the longest words, or you will get an error.
Example:
[sahaquiel#sahaquiel-PC Stackoverflow]$ cat file
correctstringERROR and more useless text
ERROR thats really error string
[sahaquiel#sahaquiel-PC Stackoverflow]$ grep -c ERROR file
2
If you wish to avoid count 2 (because counting first string, obliviously, bad way), you should also add two keys for grep:
[sahaquiel#sahaquiel-PC Stackoverflow]$ grep -cow ERROR file
1
Now you have counted only the words equal to your key, not substrings in any useful strings.
im trying to check if my script ran correctly if not, to echo a message, however when i use this if statement, it produces an error on line '9' (if [ $? -eq 0 ]) Saying that a ' is missing.
#!/bin/bash
name=$1
if ["$name" = ""]
then
echo -n "Enter a name to search for: "
read name
fi
if [ $? -eq 0 ]
then
echo "Incorrect Input"
fi
if ["$name" = ""]
is wrong because you must have a space between the [ and the expression (and before the ] as well).
There's nothing obviously wrong with the syntax of
if [ $? -eq 0 ]
but it's always dubious to check exit codes with arithmetic, since that's what if does directly. If your script is as you show it above, the second if statement should always be true. The fact that the previous if statement's expression was false doesn't matter, because the if statement yields zero if no condition tested true. For example:
$ if false; then echo hi; fi; echo $?
0
#!/bin/bash
LOCATION=$1
FILECOUNT=0
DIRCOUNT=0
if [ "$#" -lt "1" ]
then
echo "Usage: ./test2.sh <directory>"
exit 0
fi
I don't actually get what the If statement is saying can anyone help me to explain this?Thank you
$1 refers to the first argument of the bash file. In this case, you can pass your directory path by issuing the following command:
# ./test2.sh /path/of/your/directory
#!/bin/bash
LOCATION=$1 #first argument of the script
FILECOUNT=0
DIRCOUNT=0
if [ "$#" -lt "1" ] #if the number of argument(s) ($#) is less than 1
then
echo "Usage: ./test2.sh <directory>"
exit 0
fi
You can read this article for more information about parameter passing.
Hope it helps.
$1 is the first argument that is passed to the bash script. If you start the script like ./test2.sh argument1 argument2 the $1 will refer argument1.
The if-statement checks, if the count of arguments (that's the $#) is smaller than 1, then it will output the usage statement (as it seems you can't run the script without any argument).
I'm new to shell scripting and need some help. I am trying to write a script to bounce some servers and I am having a few issues with my if statements. The First and Second one below is giving me a too many arguments error.
For the first one, I am the variable $jmsProcess is a ps -ef | grep command and I only want to go into the if-statement, if this returns some results. This is the same issue for the second one.
In the Third if-statement I want it to check if either of those variables have the value true but this gives me a
if[ [ false || false ] == true ]: command not found
Error.
#Check the JMS process has been killed
if [ $jmsProcess != null ] # SHOULD THIS BE NULL???
then
echo "JMS Process is still running"
$jmsRunning = "true"
fi
#Check the Bone process has been killed
if [ $boneProcess != null ] # SHOULD THIS BE NULL???
then
echo "B-One Process is still Running"
$boneRunning = "true"
fi
if[ [ $jmsRunning || $boneRunning ] == true ] # CHECK THIS FOR QUOTES
then
# $killProcess
fi
null is not a Bash keyword. If you want to check whether a variable is defined, you can do the following:
if [ "${var+defined}" = defined ]
To check whether it's empty or undefined:
if [ -z "${var-}" ]
We don't know how you're setting any of the variable values, (jmsProcess, boneProcess).
Try surrounding all var values like "$var" (with dbl-quotes) and see if the error messages change.
Also there are numerous syntax issues in code visible above. Hard to tell if it is an artifact of posting here, (The code block feature is pretty robust), so I'm going to comment on what I see wrong.
if[ [ false || false ] == true ]: command not found
There are a lot of issues here: false is an shell command. Try typing it on the command line and then do echo $?. you should see 1. true; echo $? will return 0. But the if statements continue or fall-over to the else block based on the last return code (with some special case exceptions).
Also, I'm guessing you're trying to make some sort of reg exp with [ false || false ] == true. Won't work. see below.
You can set status variables to have the value of false (or true) which will be evaluated correctly by the shell.
Also, if[ will give the 'command not found' msg. So by using vars that have the value false, you can do
Try
jmsRunning=false ; boneRunning=true
if [[ ${jmsRunning} || ${boneRunning} ]] ; then
echo both NOT running
else
echo at least 1 is running
fi
Change both to false to see the message change.
Also, null is just a string in a shell script, you probably mean "".
Finally, var assignments cannot have spaces surrounding the '=' sign AND do not use the '$' char at the front when it is on the left hand side of the statment, i.e.
boneRunning=true
I hope this helps.