วันอังคารที่ 5 มกราคม พ.ศ. 2559

shell script

Function
#!/bin/bash
#!/bin/bash
function test {
   echo "test $1 $2 $3";
}
test a b c
 
Inner Function
$ outerfunc1() {
> innerfunc() { echo "Running inner function #1"; }
> echo "Running outer function #1"
> }
$ outerfunc2() {
> innerfunc() { echo "Running inner function #2"; }
> echo "Running outer function #2"
> }
$ # At this point, both outerfunc1 and outerfunc2 contain definitions of
$ # innerfunc, but since neither has been executed yet, the definitions
$ # haven't "happened".
$ innerfunc
-bash: innerfunc: command not found
$ outerfunc1
Running outer function #1
$ # Now that outerfunc1 has executed, it has defined innerfunc:
$ innerfunc
Running inner function #1
$ outerfunc2
Running outer function #2
$ # Running outerfunc2 has redefined innerfunc:
$ innerfunc
Running inner function #2

ref : http://stackoverflow.com/questions/8426077/how-to-define-a-function-inside-another-function-in-bash

: NOOP : No-op command
it's exit status is true (0)
http://unix.stackexchange.com/questions/37473/what-is-the-utility-of-the-command-in-shell-scripting-given-that-it-explicitl

exit N
exit 0 : normal shell exit
ref: http://bash.cyberciti.biz/guide/Exit_command

If
 if [[ true = true ]]; then
  echo "y";
 elif [[ true = false ]]; then
  :; 
 else
  echo "n";
 fi
 
Case
case "$1" in
whatever|whatever2)

;; 
*)

;; 
esac



For
for (( ; $#; )); 
do
$1
done

for A in "1 2 3"; do
 echo $A # 1 2 3 
done 
echo $A # 1 2 3
for A; do
    $A
done

Local variable
local var1 var2=()
var2 = empty array
Options : -i : make NAMEs have the "integer" attribute -a : make NAMEs indexed arrays (removing with +a is valid syntax, but leads to an error message) ref : http://unix.stackexchange.com/questions/202302/variable-definition-in-bash-using-the-local-keyword , http://wiki.bash-hackers.org/commands/builtin/declare  

Dollar sign
$@ : all of the parameters passed to the script
Example :
If you call `./someScript.sh` foo bar then `$@` will be equal to `foo bar`.
ref : http://stackoverflow.com/questions/9994295/what-does-mean-in-a-shell-script

$# : number of args
ref : http://stackoverflow.com/questions/5163144/what-are-the-special-dollar-sign-shell-variables

&2
echo "hey" >&2
print hey to &2:stderr

ref : http://stackoverflow.com/questions/23489934/echo-2-some-text-what-does-it-mean-in-shell-scripting

[[ ]],  (( ))

 ref : https://robots.thoughtbot.com/the-unix-shells-humble-if
, http://mywiki.wooledge.org/BashFAQ/031

[[ -d "/path" ]] : check if directory exists
[[ ! -d "$DIRECTORY" ]] : check if directory does not exists
[[ -r "/path" ]] : is read-only
[[ -x "/path" ]] : is executable
ref : http://stackoverflow.com/questions/59838/check-if-a-directory-exists-in-a-shell-script
, http://www.freeos.com/guides/lsst/ch03sec02.html

&&, ||
&& will only be evaluated if the exit status of the left side is zero
|| will evaluate the right side only if the left side exit status is nonzero

ref : http://unix.stackexchange.com/questions/24684/confusing-use-of-and-operators

shift
shift is a bash built-in which kind of rotates the arguments. Given that the arguments provided to the script are 3 available in $1, $2, $3, then a call to shift will make $2 the new $1. a shift 2 will shift by two makeing new $1 the old $3.
ref : http://unix.stackexchange.com/questions/174566/what-is-the-purpose-of-using-shift-in-bash-scripts

pushd, popd : directory stack
ref : http://unix.stackexchange.com/questions/77077/how-do-i-use-pushd-and-popd-commands

Read
read from stdin
-a : read the data word-wise into the specified array .
Example : use read to split string

read -r col1 col2 col3 <<< "one two three" 
printf "col1: %s col2: %s col3 %s\n" "$col1" "$col2" "$col3"
Example : changing seperator by using IFS variable
IFS=":" 

or
read -a VARNAME <<< "whatever whatever"
or
echo "press any key to continue"
read -s -n 1 -p

read from file
exec 3<$FILE # redirect file to FD 3
while read -u 3 -r line
do
 echo $line
done

or

while read -u 3 line
do
    command1
    command2
done 3< <(tail -f /path/file.txt)


ref : http://wiki.bash-hackers.org/commands/builtin/read
, http://bash.cyberciti.biz/guide/Reads_from_the_file_descriptor_%28fd%29
, http://stackoverflow.com/questions/27352868/bash-file-descriptor-3-and-while-read-line
$PWD
present working directory

Array
${ARR_NAME[@]} 
If quoted, @ expands to all elements individually quoted.
Example :
for v in "${ARR_NAME[@]}" do
$v
done 
unset ARRAY[N] : Destroys the array element at index N
http://wiki.bash-hackers.org/syntax/arrays

Int
i++
Find -or
find /path/ -name '*.png' -or -name '*.jpg'
mkdir -p -p Will create nested directories, but only if they don't exist already.
mkdir -p /foo/bar/baz  # creates bar and baz within bar under existing /foo
http://superuser.com/questions/165157/what-does-mkdir-p-flag-do

[[:alnum:]_]
letter, digit, or underscore
Example
If [[ $X != [[:alnum:]_] ]]

eval
eval - construct command by concatenating arguments

foo=10 x=foo
y='$'$x
echo $y # $foo
eval y='$'$x
echo $y # 10

ref : http://unix.stackexchange.com/questions/23111/what-is-the-eval-command-in-bash

ไม่มีความคิดเห็น:

LinkWithin

Related Posts Plugin for WordPress, Blogger...