Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
linux:shell [2008/09/08 20:27] cyril created |
linux:shell [2024/12/11 09:40] (current) cyril [Misc] |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Shell ====== | ====== Shell ====== | ||
+ | |||
+ | ===== Block comment ===== | ||
+ | |||
+ | With " | ||
+ | <code bash> | ||
+ | : << ' | ||
+ | sleep 1 | ||
+ | END | ||
+ | </ | ||
+ | |||
+ | ===== Iterate over a list as a string ===== | ||
+ | |||
+ | Different options: | ||
+ | <code bash> | ||
+ | # option 1, without variable | ||
+ | for x in a b c; do echo $x; done | ||
+ | |||
+ | # option 2, with string | ||
+ | list=" | ||
+ | for x in $list; do echo $x; done # only works with sh and bash | ||
+ | for x in ${=list}; do echo $x; done # only works with zsh | ||
+ | for x in $(echo $list}; do echo $x; done # works with any shell | ||
+ | |||
+ | # option 3, with an array | ||
+ | list=(a b c) | ||
+ | for x in $list; do echo $x; done | ||
+ | </ | ||
===== Do something recursively on all files ===== | ===== Do something recursively on all files ===== | ||
+ | Use '' | ||
+ | <code bash> | ||
+ | find . -type f -exec < | ||
+ | find -iname "< | ||
+ | </ | ||
+ | |||
+ | Manual solution: | ||
<code bash> | <code bash> | ||
#!/bin/sh | #!/bin/sh | ||
Line 29: | Line 63: | ||
search_dir " | search_dir " | ||
</ | </ | ||
+ | |||
+ | ===== Misc ===== | ||
+ | |||
+ | * **Default value for variable**: <code bash> | ||
+ | ${< | ||
+ | </ | ||
+ | * **Get directory of current script** (for instance in order to run a script that is stored in the same directory, or in a known relative path):< | ||
+ | BASEDIR=$(dirname " | ||
+ | </ | ||
+ | * **echo to stderr**, just redirect the output:< | ||
+ | echo " | ||
+ | </ | ||
+ | * **Convert dash-time to ISO** (2024-04-21_15-50-30 to 2024-04-21T15: | ||
+ | sed -r " | ||
+ | </ | ||
+ | * **Get target name of symbolic link**:< | ||
+ | readlink < | ||
+ | </ | ||
+ | * **Execute command**:< | ||
+ | var=`cat file` | ||
+ | var=$(cat file) | ||
+ | </ | ||
+ | * **Evaluate mathematical expression** ('' | ||
+ | echo $((2*3)) | ||
+ | </ | ||
+ | * **time with redirected output** (also works for groups of commands):< | ||
+ | time (cat file1 > file2) | ||
+ | time (cat file1 > /dev/null; sync) | ||
+ | </ | ||
+ | * **Check number of arguments and print usage: | ||
+ | if [[ $# -lt 2 ]]; then | ||
+ | echo " | ||
+ | exit 1 | ||
+ | fi | ||
+ | </ | ||
+ | * **Load command output as a file: | ||
+ | diff <(grep x file1) <(grep y file2) | ||
+ | </ | ||
+ | * **Remove extension from file name: | ||
+ | ${f%.png} # with all shells | ||
+ | ${f: | ||
+ | </ | ||
+ | |||
+ |