Bash shell functions Functions provide the ability to define and invoke a complex series of commands using a keyword.
functions
function rsorter () { #rsorter takes 1 or 2 arguments. # $1 is file to be sorted # $2 is file to store sorted file in # if insufficient arguments if [ $# -lt 1 ] then echo "arg count $# insufficient"; return 1; fi #test that 2nd argument not null if [ -n "$2" ] then # If $2 exists use it for output sort -r $1 -o $2 else # else use original filename for output sort -r $1 -o $1 fi } rsorter file1 rsorter file2 file3 |
function lsset () { if [ $1 == "l" ] then function ls () { /bin/ls -l } else function ls () { /bin/ls } fi } |
> lsset l > ls -rw-r--r-- 1 berezin berezin 4665 Dec 31 2013 win8.1.txt drwx------ 3 berezin berezin 4096 Jul 28 10:39 work > lsset > ls win8.1.txt work |
#!/bin/bash # fun.sh function fun2 () { for var in "$@" do echo "$var" done } fun2 "$@" |