I would like to have the equivelant of the following in a one line if/else condition.
$maxline=`cat journald.conf | grep "#SystemMaxUse="`
if [ $maxline == "#SystemMaxUse=" ]
then
sed 's/\#SystemMaxUse=/SystemMaxUse=50M/g' journald.conf > journald.conf2
mv journald.conf2 journald.conf;
else
echo "This file has been edited. You'll need to do it manually."
fi
I'm attempting to put this into a one line command. So far I've gotten it all but the else portion of the command. Here's what I have so far...
maxline=`cat journald.conf | grep "#SystemMaxUse="` && if [ $maxline == "#SystemMaxUse=" ]; then sed 's/\#SystemMaxUse=/SystemMaxUse=50M/g' journald.conf > journald.conf2 && mv journald.conf2 journald.conf; fi
So how can I include the else portion of the above code into my command? Thank you for your help in advance.
This question is related to
linux
shell
if-statement
It looks as if you were on the right track. You just need to add the else statement after the ";" following the "then" statement. Also I would split the first line from the second line with a semicolon instead of joining it with "&&".
maxline='cat journald.conf | grep "#SystemMaxUse="'; if [ $maxline == "#SystemMaxUse=" ]; then sed 's/\#SystemMaxUse=/SystemMaxUse=50M/g' journald.conf > journald.conf2 && mv journald.conf2 journald.conf; else echo "This file has been edited. You'll need to do it manually."; fi
Also in your original script, when declaring maxline you used back-ticks "`" instead of single quotes "'" which might cause problems.
To summarize the other answers, for general use:
if [ foo ]; then
a; b
elif [ bar ]; then
c; d
else
e; f
fi
if [ foo ]; then a && b; elif [ bar ]; c && d; else e && f; fi
( foo && a && b ) || ( bar && c && d ) || e && f;
Remember that the AND and OR operators evaluate whether or not the result code of the previous operation was equal to true/success (0
). So if a custom function returns something else (or nothing at all), you may run into problems with the AND/OR shorthand. In such cases, you may want to replace something like ( a && b )
with ( [ a == 'EXPECTEDRESULT' ] && b )
, etc.
Also note that (
and [
are technically commands, so whitespace is required around them.
Instead of a group of &&
statements like then a && b; else
, you could also run statements in a subshell like then $( a; b ); else
, though this is less efficient. The same is true for doing something like result1=$( foo; a; b ); result2=$( bar; c; d ); [ "$result1" -o "$result2" ]
instead of ( foo && a && b ) || ( bar && c && d )
. Though at that point you'd be getting more into less-compact, multi-line stuff anyway.
You can use like bellow:
(( var0 = var1<98?9:21 ))
the same as
if [ "$var1" -lt 98 ]; then
var0=9
else
var0=21
fi
extends
condition?result-if-true:result-if-false
I found the interested thing on the book "Advanced Bash-Scripting Guide"
It's not a direct answer to the question but you could just use the OR-operator
( grep "#SystemMaxUse=" journald.conf > /dev/null && sed -i 's/\#SystemMaxUse=/SystemMaxUse=50M/g' journald.conf ) || echo "This file has been edited. You'll need to do it manually."
Source: Stackoverflow.com