Linux If/elif/else/fi and case statement
String Comparison | Description |
---|---|
Str1 = Str2 | Returns true if the strings are equal |
Str1 != Str2 | Returns true if the strings are not equal |
-n Str1 | Returns true if the string is not null |
-z Str1 | Returns true if the string is null |
Numeric Comparison | Description |
expr1 -eq expr2 | Returns true if the expressions are equal |
expr1 -ne expr2 | Returns true if the expressions are not equal |
expr1 -gt expr2 | Returns true if expr1 is greater than expr2 |
expr1 -ge expr2 | Returns true if expr1 is greater than or equal to expr2 |
expr1 -lt expr2 | Returns true if expr1 is less than expr2 |
expr1 -le expr2 | Returns true if expr1 is less than or equal to expr2 |
! expr1 | Negates the result of the expression |
File Conditionals | Description |
-d file | True if the file is a directory |
-e file | True if the file exists (note that this is not particularly portable, thus -f is generally used) |
-f file | True if the provided string is a file |
-g file | True if the group id is set on a file |
-r file | True if the file is readable |
-s file | True if the file has a non-zero size |
-u | True if the user id is set on a file |
-w | True if the file is writable |
-x | True if the file is an executable |
In this section, we'll see couple of examples of the 'if/elif/else' statement.
Exampe #1:
#!/bin/bash # b.sh if [ "$#" -gt 0 ] then echo "There are $# args!" fi if [ "$1" = "arg1" ] then echo "argument 1 is $1" fi
Output:
$ bash b.sh arg1 arg2 arg3 There are 3 args! argument 1 is arg1
Exampe #2:
#!/bin/bash for file in *; do if [ -f $f ] then echo "file : $file" else echo "dir : $file" fi done echo echo 'find myfie1 and mydir1' for file in *; do if [ -f $file -o -d $file ] then if [ $file = 'myfile1' ] then echo $file elif [ $file = 'mydir1' ] then echo $file else echo '.' fi fi done
Output:
$ bash c.sh file : a.sh file : b.sh file : c.sh file : dir1 file : dir2 file : mydir1 file : mydir2 file : myfile1 file : myfile2 find myfie1 and mydir1 . . . . . mydir1 . myfile1 .
Syntax of bash case statement:
case expression in pattern1 ) statements ;; pattern2 ) statements ;; ... esac
Example:
#! /bin/bash ARCH=$(uname -m) case $ARCH in armv6*) ARCH="armv6";; armv7*) ARCH="arm";; aarch64) ARCH="arm64";; x86) ARCH="386";; x86_64) ARCH="amd64";; i686) ARCH="386";; i386) ARCH="386";; esac echo "ARCH=$ARCH"
Output:
ARCH=amd64
Linux kernel implements about 30 signals. Each signal identified by a number, from 1 to 31. Signals don't carry any argument and their names are mostly self explanatory. For instance SIGKILL or signal number 9 tells the program that someone tries to kill it, and SIGHUP used to signal that a terminal hangup has occurred, and it has a value of 1.
Here is an example of the "case" statement that kill a "sleep" process by sending signals and it's PID.
#! /bin/bash if [ $# -lt 2 ] then echo "Usage: $0 signal# PID" exit fi case $1 in 1) echo "SIGHUP" kill -SIGHUP $2 ;; 2) echo "SIGINT" kill -SIGINT $2 ;; 3) echo "SIGQUIT" kill -SIGQUIT $2 ;; 9) echo "SIGKILL" kill -SIGKILL $2 ;; *) echo "signal# $1 not processed";; esac
Note: $# is the count of the arguments passed to the script.
Output on terminal #1:
$ sleep 100 $ sleep 100 Quit: 3
Output on terminal #2:
$ ps aux|grep sleep|grep -v grep ki.hong 11762 0.0 0.0 4267916 652 s000 S+ 8:58AM 0:00.02 sleep 100 $ ./signal.sh 3 11762 SIGQUIT
Linux - system, cmds & shell
- Linux Tips - links, vmstats, rsync
- Linux Tips 2 - ctrl a, curl r, tail -f, umask
- Linux - bash I
- Linux - bash II
- Linux - Uncompressing 7z file
- Linux - sed I (substitution: sed 's///', sed -i)
- Linux - sed II (file spacing, numbering, text conversion and substitution)
- Linux - sed III (selective printing of certain lines, selective definition of certain lines)
- Linux - 7 File types : Regular, Directory, Block file, Character device file, Pipe file, Symbolic link file, and Socket file
- Linux shell programming - introduction
- Linux shell programming - variables and functions (readonly, unset, and functions)
- Linux shell programming - special shell variables
- Linux shell programming : arrays - three different ways of declaring arrays & looping with $*/$@
- Linux shell programming : operations on array
- Linux shell programming : variables & commands substitution
- Linux shell programming : metacharacters & quotes
- Linux shell programming : input/output redirection & here document
- Linux shell programming : loop control - for, while, break, and break n
- Linux shell programming : string
- Linux shell programming : for-loop
- Linux shell programming : if/elif/else/fi
- Linux shell programming : Test
- Managing User Account - useradd, usermod, and userdel
- Linux Secure Shell (SSH) I : key generation, private key and public key
- Linux Secure Shell (SSH) II : ssh-agent & scp
- Linux Secure Shell (SSH) III : SSH Tunnel as Proxy - Dynamic Port Forwarding (SOCKS Proxy)
- Linux Secure Shell (SSH) IV : Local port forwarding (outgoing ssh tunnel)
- Linux Secure Shell (SSH) V : Reverse SSH Tunnel (remote port forwarding / incoming ssh tunnel) /)
- Linux Processes and Signals
- Linux Drivers 1
- tcpdump
- Linux Debugging using gdb
- Embedded Systems Programming I - Introduction
- Embedded Systems Programming II - gcc ARM Toolchain and Simple Code on Ubuntu/Fedora
- LXC (Linux Container) Install and Run
- Linux IPTables
- Hadoop - 1. Setting up on Ubuntu for Single-Node Cluster
- Hadoop - 2. Runing on Ubuntu for Single-Node Cluster
- ownCloud 7 install
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox I
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox II
- Windows 8 guest on Mac OSX host using VirtualBox I
- Ubuntu Package Management System (apt-get vs dpkg)
- RPM Packaging
- How to Make a Self-Signed SSL Certificate
- Linux Q & A
- DevOps / Sys Admin questions
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization