Inline if shell script
It doesn't work because you missed out fi
to end your if
statement.
counter=`ps -ef | grep -c "myApplication"`; if [ $counter -eq 1 ]; then echo "true"; fi
You can shorten it further using:
if [ $(ps -ef | grep -c "myApplication") -eq 1 ]; then echo "true"; fi
Also, do take note the issue of ps -ef | grep ...
matching itself as mentioned in @DigitalRoss' answer.
update
In fact, you can do one better by using pgrep
:
if [ $(pgrep -c "myApplication") -eq 1 ]; then echo "true"; fi
One line if/else condition in linux shell scripting
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.
Is there an inline-if with assignment (ternary conditional) in bash?
There is no ?:
conditional operator in the shell, but you could make the code a little less redundant like this:
if [ $useDefault ]; then
tmpname="$defaultName"
else
tmpname="$customName"
fi
fileName="$dirName/$tmpname.txt"
Or you could write your own shell function that acts like the ?:
operator:
cond() {
if [ "$1" ] ; then
echo "$2"
else
echo "$3"
fi
}
fileName="$dirname/$(cond "$useDefault" "$defaultName" "$customName").txt"
though that's probably overkill (and it evaluates all three arguments).
Thanks to Gordon Davisson for pointing out in comments that quotes nest within $(...)
.
How to write if else in one line in shell?
Space -- the final frontier. This works:
if [ $SERVICESTATEID -eq 2 ]; then echo "CRITICAL"; else echo "OK"; fi
Note spaces after [
and before ]
-- [
is a command name! And I removed an extra $
at the end of $SERVICESTATEID
.
An alternative is to spell out test
. Then you don't need the final ]
, which is what I prefer:
if test $SERVICESTATEID -eq 2; then echo "CRITICAL"; else echo "OK"; fi
Bash: If/Else statement in one line
There is no need to explicitly check $?
. Just do:
ps aux | grep some_proces[s] > /tmp/test.txt && echo 1 || echo 0
Note that this relies on echo not failing, which is certainly not guaranteed. A more reliable way to write this is:
if ps aux | grep some_proces[s] > /tmp/test.txt; then echo 1; else echo 0; fi
Capture and check return status inline in if statement inside [[ ]]
This does not make sense:
if cmd ; ...
would run cmd
and then branch depending on the exit status of cmd.
Similarily,
if [[ cmd ]]; ....
runs the command [[...]] and branch accordingly. It does not run cmd
.
Actually, as long as you are only interested in knowing whether the exit status is zero or not, I see no reason why you should fiddle around with $?
. Things are different if you have several exit status values to distinguish. For example:
grep foo bar; status=$?
if (( status == 0 ))
then
# actions where grep found the pattern
elif (( status == 1 ))
# actions where grep did not find the pattern
else
# actions where grep encountered a serious problem
fi
In this case, store the exit code immediately after invoking your command into some variable, and process it later with if
or case
or whatever you need.
Ternary operator (?:) in Bash
ternary operator ? :
is just short form of if/else
case "$b" in
5) a=$c ;;
*) a=$d ;;
esac
Or
[[ $b = 5 ]] && a="$c" || a="$d"
Related Topics
What Context Does the Scheduler Code Run In
What Does the Mkdir -P Mean in a Script File
How and When to Use /Dev/Shm for Efficiency
Running Apt-Get for Another Partition/Directory
Testing Out of Disk Space in Linux
When Should I Write a Linux Kernel Module
Linux: Getting Umask of an Already Running Process
Clean Way of Launching a Shell Script in Background from Jenkins
Linux Loopback Performance with Tcp_Nodelay Enabled
Iproute2 Commands for Mpls Configuration
How to Input Password to Git Pull Command
Linux "Free -M": Total, Used and Free Memory Values Don't Add Up
How to Delete History of Last 10 Commands in Shell
Tool to Visualize the Device Tree File (Dtb) Used by the Linux Kernel