Difference between revisions of "Short Notes on Bash"
From PaskvilWiki
(Created page with "== Extensions and File Name == <pre>$ fullname='/tmp/file.tar.gz' $ filename=$(basename $fullname) $ echo ${filename##*.} >> gz $ echo ${filename#*.} >> tar.gz $ echo ${filename...") |
|||
| Line 13: | Line 13: | ||
For more options and explanations, see [http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html Bash - Shell Parameters Expansion]. | For more options and explanations, see [http://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html Bash - Shell Parameters Expansion]. | ||
| + | |||
| + | == File Test Operators == | ||
| + | |||
| + | {| class="wikitable" | ||
| + | |- | ||
| + | ! Operator !! Function | ||
| + | |- | ||
| + | | -e || file exists | ||
| + | |- | ||
| + | | -f || exists and is ''regular'' file (not directory or device file) | ||
| + | |- | ||
| + | | -s || exists and is non-zero size | ||
| + | |- | ||
| + | | -d || exists and is directory | ||
| + | |- | ||
| + | | -b || exists and is block device | ||
| + | |- | ||
| + | | -c || exists and is character device | ||
| + | |- | ||
| + | | -p || exists and is pipe | ||
| + | |- | ||
| + | | -h || exists and is symbolic link | ||
| + | |- | ||
| + | | -S || exists and is socket | ||
| + | |- | ||
| + | | -t || is associated with terminal device; e.g. <tt>[ -t 0 ]</tt> and <tt>[ -t 1 ]</tt> test whether stdin and stdout is terminal | ||
| + | |- | ||
| + | | -r || user has read permission | ||
| + | |- | ||
| + | | -w || user has write permission | ||
| + | |- | ||
| + | | -x || user has execute permission | ||
| + | |- | ||
| + | | -O || user is owner | ||
| + | |- | ||
| + | | -G || user's group is the same as the file's group | ||
| + | |- | ||
| + | | -N || file modified since it was last read | ||
| + | |- | ||
| + | | f1 -nt f2 || ''f1'' is newer than ''f2'' | ||
| + | |- | ||
| + | | f1 -ot f2 || ''f1'' is older than ''f2'' | ||
| + | |} | ||
Latest revision as of 02:58, 28 October 2013
Extensions and File Name
$ fullname='/tmp/file.tar.gz'
$ filename=$(basename $fullname)
$ echo ${filename##*.}
>> gz
$ echo ${filename#*.}
>> tar.gz
$ echo ${filename%.*}
>> file.tar
$ echo ${filename%%.*}
>> file
For more options and explanations, see Bash - Shell Parameters Expansion.
File Test Operators
| Operator | Function |
|---|---|
| -e | file exists |
| -f | exists and is regular file (not directory or device file) |
| -s | exists and is non-zero size |
| -d | exists and is directory |
| -b | exists and is block device |
| -c | exists and is character device |
| -p | exists and is pipe |
| -h | exists and is symbolic link |
| -S | exists and is socket |
| -t | is associated with terminal device; e.g. [ -t 0 ] and [ -t 1 ] test whether stdin and stdout is terminal |
| -r | user has read permission |
| -w | user has write permission |
| -x | user has execute permission |
| -O | user is owner |
| -G | user's group is the same as the file's group |
| -N | file modified since it was last read |
| f1 -nt f2 | f1 is newer than f2 |
| f1 -ot f2 | f1 is older than f2 |