Linux shell programming : Test
In shell, test is called as [:
$ ls /bin [ chmod date domainname expr ksh ln mv pwd sh sync unlink bash cp dd echo hostname launchctl ls pax rm sleep tcsh wait4path cat csh df ed kill link mkdir ps rmdir stty test zsh
'[' is actually a program, just like 'ls' and other programs, so it must be surrounded by spaces. So, the following code:
if [ $foo = "bar" ]
will be translated into:
if SPACE [ SPACE "$foo" SPACE = SPACE "bar" SPACE ]
Note that '[' is a program and need a space, the following won't work:
if [$foo = "bar" ]
That's because it is interpreted as the following:
if test$foo = "bar" ]
test
is most often used with the if statement:
if [ ... ]; then echo "do something" elif [ something_else ]; then echo "something else" else echo "none of the above" fi
If we want to perform a different action based on whether the file exists or not simply use the if/then construct:
# t.sh #!/bin/bash FILE=/etc/resolv.conf if [ -f $FILE ]; then echo "$FILE exist" else echo "$FILE does not exist" fi
$ ./t.sh /etc/resolv.conf exist
Actually, the test
command takes one of the following syntax forms:
test EXPRESSION [ EXPRESSION ] [[ EXPRESSION ]]
So, the following script behaves exactly the same way:
#!/bin/bash FILE=/etc/resolv.conf if test -f $FILE; then echo "$FILE exist" else echo "$FILE does not exist" fi
We can also use the test
command without the if statement.
The command after the && operator will only be executed if the exit status of the test command is true:
$ FILE=/etc/resolv.conf $ test -f $FILE && echo "$FILE exists" /etc/resolve.conf exists $ [ -f $FILE ] && echo "$FILE exists" /etc/resolve.conf exists $ [[ -f $FILE ]] && echo "$FILE exists" /etc/resolve.conf exists
We can use the operators -d to test whether a file is a directory or not:
# d.sh #!/bin/bash FILE=/etc/apache2 if [ -d $FILE ]; then echo "$FILE is a directory" fi
$ ./d.sh /etc/apache2 is a directory
$ FILE=/etc/apache2 $ test -d $FILE && echo "$FILE is a directory" /etc/apache2 is a directory $ [ -d $FILE ] && echo "$FILE is a directory" /etc/apache2 is a directory
We can check if a file does not exist:
# t.sh #!/bin/bash FILE=/etc/resolv.conf if [ ! -f $FILE ]; then echo "$FILE does not exist" else echo "$FILE exist" fi
$ ./t.sh /etc/resolv.conf exist
-b FILE
- True if the FILE exists and is a block special file.-c FILE
- True if the FILE exists and is a special character file.-d FILE
- True if the FILE exists and is a directory.-e FILE
- True if the FILE exists and is a file, regardless of type (node, directory, socket, etc.).-f FILE
- True if the FILE exists and is a regular file (not a directory or device).-G FILE
- True if the FILE exists and has the same group as the user running the command.-h FILE
- True if the FILE exists and is a symbolic link.-g FILE
- True if the FILE exists and has set-group-id (sgid) flag set.-k FILE
- True if the FILE exists and has a sticky bit flag set.-L FILE
- True if the FILE exists and is a symbolic link.-O FILE
- True if the FILE exists and is owned by the user running the command.-p FILE
- True if the FILE exists and is a pipe.-r FILE
- True if the FILE exists and is readable.-S FILE
- True if the FILE exists and is socket.-s FILE
- True if the FILE exists and has nonzero size.-u FILE
- True if the exists and set-user-id (suid) flag is set.-w FILE
- True if the FILE exists and is writable.-x FILE
- True if the FILE exists and is executable.
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