s
) that must not be mistaken for getopts. Watch for typos !OPTIND
. OPTIND
is initialized to 1 each time the shell or a shell script is invoked.OPTARG
. The shell does not reset OPTIND
automatically; it must be manually reset between multiple calls to getopts within the same shell invocation if a new set of parameters is to be used.while
loop).while
loop.
while
loop.:fA:x it's legal to have several:
in the optstring ^ ^ | +----:
following an option : that option requires a value +------- leading:
silent mode
fAx
means that options -f
, -A
and -x
are expected:
) :
fA:x
means that -A
has a value, i.e. the command line will look like myScript.sh -A value verbose mode | silent mode | |
---|---|---|
enabled with | enabled by default | a leading colon :fA:x |
$OPTARG on invalid option |
unset, which means :
|
the invalid option character |
#!/usr/bin/env bash while getopts ':a' opt; do case "$opt" in a) echo '-a was triggered!' ;; \?) echo "Invalid option: -$OPTARG" ;; esac done
#!/usr/bin/env bash while getopts ':a:' opt; do case "$opt" in a) echo "Value of option '$opt' : '$OPTARG'" ;; \?) echo "Invalid option: -$OPTARG" ;; esac done
#!/usr/bin/env bash while getopts ':a:' opt; do same as above case "$opt" in a) echo "✅ Option 'a' has value '$OPTARG'" ;; :) echo "⚠️ No value given for option '-$OPTARG'"; exit 1 ;; missing value handled here \?) echo "⛔ Invalid option: '-$OPTARG'"; exit 1 ;; esac done
#!/usr/bin/env bash options=':ab' if (! getopts "$options" opt); then Read more about this below echo 'no option given' exit 1 fi while getopts "$options" opt; do case "$opt" in a) echo "'-a' was triggered!" ;; b) echo "'-b' was triggered!" ;; \?) echo "Invalid option: '-$OPTARG'"; exit 1 ;; esac done
while
loop. If we replace the whole if
block with getopts "$options" opt || { echo 'no option given'; exit 1; }
it "swallows" the first option :
./test.sh no option given as expected ./test.sh -ab '-b' was triggered! where's thea
? ./test.sh -aab '-a' was triggered! Houston, we've hada
problem '-b' was triggered!
cat << EOF >> /etc/bash.bashrc alias gzip='pigz' alias gunzip='unpigz' EOF
Flag | Usage |
---|---|
-c --stdout --to-stdout | Write output on standard output and keep original files unchanged.
gzip someFile replaces someFile with its compressed counterpart someFile.gz. With -c, you may run :gzip -c someFile > whatever to have both the compressed and uncompressed versions of someFile.
gzip stores the original file name into the compressed file so that gunzip can recover it (check it with : file whatever). However, mixing gzip -c and gunzip (without -c) produces unexpected results.
regularFile=$(mktemp --tmpdir regular.XXXXXXXX); compressedFile=$(mktemp --tmpdir compressed.XXXXXXXX); for i in {1..100}; do echo "$i : Hello world" >> "$regularFile"; done; gzip -c "$regularFile" > "$compressedFile"; ls -lhU "$regularFile" "$compressedFile"; file "$compressedFile"; gunzip "$compressedFile"; rm "$regularFile" "$compressedFile" -rw------- 1 bob users 1.7K Oct 29 11:18 /tmp/regular.JS4MvZYv -rw------- 1 bob users 255 Oct 29 11:18 /tmp/compressed.af0tDooE /tmp/compressed.af0tDooE: gzip compressed data, was "regular.JS4MvZYv", last modified: Sat Oct 29 11:18:41 2016, from Unix gzip: /tmp/compressed.af0tDooE: unknown suffix -- ignored |
--rsyncable | Make the compressed file rsync-friendly. gzip uses adaptative compression methods in which changing a single byte of the source data may cascade many changes in the compressed file. As a result, this would defeat Rsync's "send-only-changed-bytes" strategy. The --rsyncable disables the adaptative compression. (details) "rsyncable" compressed files are less than 1% larger than non-rsyncable files
gunzip makes no difference between rsyncable / not rsyncable files.
|
-n --fast --best |
Specify the compression level n :
|
Flag | Usage |
---|---|
-g gid | assign the group ID gid for the new group |
-r --system | create a system group |
Binary file someFile matches
Binary file.*matches
' && echo -e "$binaryFile\n"; doneCode | Condition | Details |
---|---|---|
0 | some match was found | |
1 | no match found | |
2 | an error occurred. | Except if a match is found while errors are silenced by the quiet mode. |
Flag | Usage |
---|---|
-An --after-context=n | display the n lines after the one matching the pattern |
-a --text | process a binary file as if it were text (see note above) |
-Bn --before-context=n | display the n lines before the one matching the pattern |
-b --byte-offset | print the 0-based byte offset within the input file before each line of output With -o : print the offset of the matching part itself. |
-Cn --context=n | equivalent to -Bn -An |
-c --count | print a count of matching lines for each input file. To count all occurrences within input (i.e. several occurrences per line), see this. |
--color=value | highlight matched string. value can be :
|
-d action --directories=action |
if the input is a directory, use action to process it. action can be :
|
-e pattern |
|
-E --extended-regexp |
consider the given pattern as an extended regular expression. grep -E is equivalent to egrep
|
--exclude=pattern |
|
--exclude-dir=pattern |
* , ? , and [] as wildcards, and \ to quote a wildcard or backslash character literally
|
-F pattern --fixed-strings pattern |
Interpret pattern as fixed strings, not regular expressions
makes grep much faster
|
-f file --file=file |
get patterns from file, one per line |
-G --basic-regexp | Interpret the pattern as a basic regular expression. This is the default. |
-h --no-filename | do not show the name of the file where a match is found. This is the default when grepping a single file |
-H --with-filename | print the name of the file where a match is found. This is the default when grepping several files |
-i --ignore-case | ignore case |
--include=pattern |
|
-l --files-with-matches | show only the name of files having a match. By default, grep outputs the file name and every matched line.
"list matching files"
|
-L --files-without-match | show only the name of files having no match |
-m n --max-count=n | Stop reading input after n matching lines (example)
grep -m n pattern is a good alternative to grep pattern | head -n
|
-n --line-number | prefix each line of output with the line number within its input file |
-o --only-matching | show only the part of a matching line that matches the pattern |
-P pattern --perl-regexp pattern |
consider pattern as a Perl regEx
This option is experimental when combined with the -z / --null-data option.
|
-q --quiet --silent | do not write anything to standard output (Read more). |
-r --recursive |
|
-R | like -r but follows symlinks |
-s --no-messages |
suppress error messages about nonexistent or unreadable files
This is fine to workaround the grep: myDir: Is a directory message, but you should consider -d
|
-v --invert-match | invert the sense of matching to select non-matching lines |
-w word --word-regexp word |
return lines containing matches that form whole words. word matches either at the beginning/end of line, and between characters that are not letters, digits or the underscore. |
-x --line-regexp |
return only results that exactly match the whole line. For a regular expression pattern (-E pattern), this is like parenthesizing the pattern and then surrounding it with ^ and $ .
echo -e 'foo\nbar\nbaz\nfoobar\nfoobaz\nbarbaz' | grep -x '
bar 'bar bar |
2
1
2
4
b
'; echo $?b 0
b
'; echo $?0
z
'; echo $?1
z
'; echo $?1
Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information. 2
Usage: grep [OPTION]... PATTERN [FILE]... Try 'grep --help' for more information. 2
0
1
2
bob : bob adm dialout cdrom plugdev lpadmin admin sambashare
getent lets admins get entries in a number of important text files called databases. This includes the passwd and group databases which store user information.
Since getent uses the same name service as the system, it will show all information, including that gained from network information sources such as LDAP.
The databases it searches in are :
Code | Condition | Details |
---|---|---|
0 | Command completed successfully | |
2 | No result found |
root:x:0:0:root:/root:/bin/bash
^
and :
in the grep command are there to make sure we hit the username field of /etc/passwd . getent is much less error-prone.passwd
refers to the passwd database.mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash
tanderson:*:1234567890:3456789012:Thomas ANDERSON:/home/tanderson:/bin/bash
tanderson:*:1234567890:3456789012:Thomas ANDERSON:/home/tanderson:/bin/bash
[domain/myDomain]
enumerate = true
myGroup:x:myGroupId:joe,jack,william,averellOtherwise, it displays nothing and returns a Unix failure.