Bash Scripting: Difference between revisions
m Text replacement - "</code>" to "}}" |
No edit summary |
||
| Line 1: | Line 1: | ||
== Test == | |||
{| | |||
! {{code|-e}} | |||
| file exists | |||
| | |||
|- | |||
! {{code|-a}} | |||
| file exists (deprecated) | |||
This is identical in effect to {{code|-e}} | |||
|- | |||
! {{code|-f}} | |||
| file is a regular file (not a directory or device file) | |||
|- | |||
! {{code|-s}} | |||
| file is not zero size | |||
|- | |||
! {{code|-d}} | |||
| file is a directory | |||
|- | |||
! {{code|-b}} | |||
| file is a block device. Eg. {{code|/dev/sda}}. | |||
|- | |||
! {{code|-c}} | |||
| file is a character device. Eg. {{code|/dev/tty0}}. | |||
|- | |||
! {{code|-p}} | |||
| file is a pipe. Eg. {{code|/dev/fd/0}}. | |||
|- | |||
! {{code|-h}} | |||
| file is a symbolic link | |||
|- | |||
! {{code|-L}} | |||
| file is a symbolic link | |||
|- | |||
! {{code|-S}} | |||
| file is a socket | |||
|- | |||
! {{code|-t}} | |||
| file (descriptor) is associated with a terminal device | |||
This test option may be used to check whether the stdin [ -t 0 ] or stdout [ -t 1 ] in a given script is a terminal. | |||
|- | |||
! {{code|-r}} | |||
| file has read permission (for the user running the test) | |||
|- | |||
! {{code|-w}} | |||
| file has write permission (for the user running the test) | |||
|- | |||
! {{code|-x}} | |||
| file has execute permission (for the user running the test) | |||
|- | |||
! {{code|-g}} | |||
| file or directory has set-group-id (sgid) flag set | |||
If a directory has the sgid flag set, then a file created within that directory belongs to the group that owns the directory. | |||
|- | |||
! {{code|-u}} | |||
| file has set-user-id (suid) flag set | |||
|- | |||
! {{code|-k}} | |||
| file or directory has sticky bit set | |||
* If set on a file, that file will be kept in cache memory, for quicker access. | |||
* If set on a directory, it restricts write permission. Setting the sticky bit adds a t to the permissions on the file or directory listing. This restricts altering or deleting specific files in that directory to the owner of those files. | |||
|- | |||
! {{code|-O}} | |||
| you are owner of file | |||
|- | |||
! {{code|-G}} | |||
| group-id of file same as yours | |||
|- | |||
! {{code|-N}} | |||
| file modified since it was last read | |||
|- | |||
! {{code|f1 -nt f2}} | |||
| file f1 is newer than f2 | |||
|- | |||
! {{code|f1 -ot f2}} | |||
| file f1 is older than f2 | |||
|- | |||
! {{code|f1 -ef f2}} | |||
| files f1 and f2 are hard links to the same file | |||
|- | |||
! {{code|!}} | |||
| "not" -- reverses the sense of the tests above (returns true if condition absent). | |||
|} | |||
== String Matching == | == String Matching == | ||
=== Regex === | === Regex === | ||
| Line 114: | Line 205: | ||
== Snippets == | == Snippets == | ||
=== Get First IP Address === | === Get First IP Address === | ||
{{highlight|lang=terminal|code= | |||
# ip a | /bin/grep inet | grep -v inet6 | tail -n+2 | head -n 1 | awk '{print $2}' | cut -d'/' -f1 | |||
}} | |||
[[Category:Linux]] | [[Category:Linux]] | ||
{{Navbox Linux}} | {{Navbox Linux}} | ||
Revision as of 19:56, 24 May 2019
Test
-e
|
file exists | |
|---|---|---|
-a
|
file exists (deprecated)
This is identical in effect to | |
-f
|
file is a regular file (not a directory or device file) | |
-s
|
file is not zero size | |
-d
|
file is a directory | |
-b
|
file is a block device. Eg. /dev/sda.
| |
-c
|
file is a character device. Eg. /dev/tty0.
| |
-p
|
file is a pipe. Eg. /dev/fd/0.
| |
-h
|
file is a symbolic link | |
-L
|
file is a symbolic link | |
-S
|
file is a socket | |
-t
|
file (descriptor) is associated with a terminal device
This test option may be used to check whether the stdin [ -t 0 ] or stdout [ -t 1 ] in a given script is a terminal. | |
-r
|
file has read permission (for the user running the test) | |
-w
|
file has write permission (for the user running the test) | |
-x
|
file has execute permission (for the user running the test) | |
-g
|
file or directory has set-group-id (sgid) flag set
If a directory has the sgid flag set, then a file created within that directory belongs to the group that owns the directory. | |
-u
|
file has set-user-id (suid) flag set | |
-k
|
file or directory has sticky bit set
| |
-O
|
you are owner of file | |
-G
|
group-id of file same as yours | |
-N
|
file modified since it was last read | |
f1 -nt f2
|
file f1 is newer than f2 | |
f1 -ot f2
|
file f1 is older than f2 | |
f1 -ef f2
|
files f1 and f2 are hard links to the same file | |
!
|
"not" -- reverses the sense of the tests above (returns true if condition absent). |
String Matching
Regex
Use the new test (ie: x =~ regex ) to test regular expressions.
[[ zone36-ta =~ .*ta$ ]] && echo matched
Or use the new test with wildcards.
[[ zone36-ta =~ *ta ]] && echo matched
Useful Functions / Utilities
Here are some common functions I use in my scripts.
Prompt
You can prompt the user for an yes or no answer using this function. An optional default values are accepted as the second argument.
Example Usage:
if prompt "Is this okay?" Y ; then
echo "It's Okay!"
fi
if ! prompt "Is this okay?" N ; then
echo "It's not okay!"
fi
Source: <phorkie>https://phorkie.leo.home.steamr.com/13/embed</phorkie>
die
Similar to the die command in PHP which prints out a message before stopping execution.
Function:
function die {
echo $@
exit
}
Caveat when using in a subshell
This will not work inside a subshell as it will just quit the subshell rather than the script. To get around this, you may need to kill the parent PID instead.
Indent
Indents text piped to the function.
Source: <phorkie>https://phorkie.leo.home.steamr.com/14/embed</phorkie>
Usage:
echo "test" | Indent
test
echo "test" | Indent 2
test
Extracting / Parsing Filenames
Use the basename to extract filenames from a path and and dirname to extract the path of a filename.
Eg:
$ dirname /etc/hosts
/etc
$ basename /etc/hosts
hosts
To extract only the extension or the filename without the extension, use bash's string matching:
Filename=$(basename /etc/resolv.conf)
Ext="${Filename##*.}"
Name="${Filename%.*}"
Arithmetic Operations
Do integer arithmetic operations on variables within double brackets. For example:
Value=1
ValuePlusOne=$((Value + 1))
ValueTimes12=$((Value * 12))
The operators that are supported are:
+ - / * %
Bitwise operators also work:
^ (XOR) | (OR) & (AND)
Control Blocks
Switch
read -p "What do you want to do?: " Option
case "$Option" in
"K")
install_new_kernel
die "Complete!"
;;
"H")
usage
;;
*)
die "Stopping installation."
;;
esac
Snippets
Get First IP Address
# ip a