bash

Import (include) a file in bash script

Use

source [file]

or use

. [file]

Call a function dynamically

create_repo_from_$TYPE $SRC_PATH $DEST_PATH

extglob

http://stackoverflow.com/questions/216995/how-can-i-use-inverse-or-negative-wildcards-when-pattern-matching-in-a-unix-linu

 

find . -name '*ettercap*' exec rm -rf "{}"\;
or
find . -name '*ettercap*' | xargs rm -rf
Note that this will work incorrectly if there are any filenames containing newlines or spaces. To deal with filenames containing newlines or spaces you should use instead:
find . -name '*ettercap*' -print0 | xargs -0 rm

  1. $1, $2, $3, … are the positional parameters.
  2. "$@" is an array-like construct of all positional parameters, {$1, $2, $3 ...}.
  3. "$*" is the IFS expansion of all positional parameters, $1 $2 $3 ....
  4. $# is the number of positional parameters.
  5. $- current options set for the shell.
  6. $$ pid of the current shell (not subshell).
  7. $_ most recent parameter (or the abs path of the command to start the current shell immediately after startup).
  8. $IFS is the (input) field separator.
  9. $? is the most recent foreground pipeline exit status.
  10. $! is the PID of the most recent background command.
  11. $0 is the name of the shell or shell script.buy.html

https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html

Add strings to file

cat >> /etc/profile << EOF
export GENERATE_GEP_CODE=”$GENERATE_GEP_CODE”
export GENERATE_BLADE_CODE=”$GENERATE_BLADE_CODE”
export ENUX=”ENUX”
export BOND_HASH_POLICY=”$BOND_HASH_POLICY”
export SHELF_ENV=”$SHELF_ENV”
EOF

TODO

exec 1>stdout.log 2>stderr.log

bash : Self redirect of Script’s output to a file

Send string to stdin

Command read one line of string from stdin

zic -d ~/tmp - <<< "Zone UTC+1015 10:15 - UTC+10:15"

The - is to read from stdin instead of file.
or read multiple lines of string

zic -d ~/tmp - << EOF
Zone UTC+1015 10:15 - UTC+10:15
EOF

Some commands don’t need -

cat <<< "Hello"
cat - <<< "Hello"

work the same

Or we can use echo

echo "Zone UTC+1015 10:15 - UTC+10:15" | zic -d ~/tmp -

Redirect from a command

diff <(ls /bin) <(ls /usr/bin)

Read lines from file

while read line
do
   echo =$line=
done < /boot/grub/grub.cfg

References
http://stackoverflow.com/questions/20307299/linux-why-cant-i-pipe-find-result-to-rm
http://stackoverflow.com/questions/6541109/send-string-to-stdin

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s