Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Bash Scripting

3,296 views

Published on

Published in: Technology
  • Follow the link, new dating source: ♥♥♥ http://bit.ly/2F7hN3u ♥♥♥
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Dating for everyone is here: ❶❶❶ http://bit.ly/2F7hN3u ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Bash Scripting

  1. 1. Bash Scripting ➢ Кaкво е това нещо bash ➢ За какво може да се използва ➢ Как да разберем кога нещо не е за bash
  2. 2. Bash Scripting Ресурси от където можете да започнете ➢ http://www.tldp.org/HOWTO/ Bash-Prog-Intro-HOWTO.html --- man bash --- ➢ Advanced bash scripting http://www.tldp.org/LDP/abs/html/
  3. 3. Bash Scripting Прост скрипт на bash #!/bin/bash STR="Hello World!" echo $STR
  4. 4. Bash Scripting Какво ни е нужно за да си напишем хубав скрипт ➢ ясно дефиниране на задачата ➢ познаване на командите които ще ползвате за да се свърши задачата ➢ свободно време :)
  5. 5. Bash Scripting Видове скриптове ➢ one line ps aux|grep root|awk '{print $2}' ➢ multiline string='2' let string++ echo $string
  6. 6. Bash Scripting Variables ➢ export, typeset, declare, readonly ➢ local, global function x { local USER='root' }
  7. 7. Bash Scripting Variables deference ➢ $a ➢ file_no$a ➢ $afile_no – does not work :) ➢ ${a}file_no – this is the way to do it
  8. 8. Bash Scripting File descriptors(std=standard): stdin – uses pipe or < stdout – uses > stderr – uses 2>
  9. 9. Bash Scripting Какво можете да правите с тези file descriptor-и: 1. redirect stdout to a file ( ls -l > ls-l.txt ) 2. redirect stderr to a file ( grep da * 2> grep-errors.txt ) 3. redirect stdout to a stderr ( grep da * 1>&2 ) 4. redirect stderr to a stdout ( grep * 2>&1 ) 5. redirect stderr and stdout to a file ( rm -f $(find / -name core) &> /dev/null )
  10. 10. Bash Scripting Pipes ls -l | grep aha ps aux | grep userx | awk '{print $2}' cat filename | sed 's/krava/mlqko/g'
  11. 11. Bash Scripting #!/bin/bash tar -cfz /usr/local/backup/my-backup-today.tgz /home/me #!/bin/bash dir='/usr/local/backup' file=$dir/my-backup-$(date+%Y%m%d).tgz tar -cfz $file /home/me
  12. 12. Bash Scripting debuging най дорият начин: #!/bin/bash -x за големи скриптове echo или read
  13. 13. Bash Scripting Контролни структори if-then-else if-then-elif-then-elif-then-else select-in case-in for-do while-do until-do
  14. 14. Bash Scripting if-then-else if [ “$1” == '' ]; then echo “Usage: $0 variable” else make_me_stop fi if ( ! ps ax | grep kuku > /dev/null ); then echo “KUKU not found!” fi
  15. 15. Bash Scripting Difference in parenthesis ( ) - executes commands in new shell { } - executes commands in the current shell (( )) - arithmetic expressions [ ] - basic integer arithmetic, basic string comparison and file attributes checks [[ ]] - basic integer arithmetic, regular expressions and file attributes checks
  16. 16. Bash Scripting if-then-elif-then-else if [ "$1" == 'weekly' ]; then table_list=$(<tables.weekly) elif [ "$1" == 'daily' ]; then table_list=$(<tables.daily) else table_list=$(<tables.hourly) fi tables='' for table in $table_list; do tables="$tables $table"; done mysqldump --compact -e -t -q -R --single-transaction blog_db $tables
  17. 17. Bash Scripting case-in case “$1” in 'start') make_me_stop ;; 'stop') make_me_start ;; *) echo “Usage: $0 variable” ;; esac
  18. 18. Bash Scripting for-do for var in `seq 1 10`; do echo -n “$var “ done Ето какво ще изведе това: 1 2 3 4 5 6 7 8 9 10 for file in /bin/*; do echo $file done Това е еквивалентно на ls -1A /bin
  19. 19. Bash Scripting while-do count=0; while(true); do let count++; if [ “$count” == “10” ]; then exit 0; fi done
  20. 20. Bash Scripting while-do count=0; while(true); do let count++; if [ “$count” == “10” ]; then exit 0; fi done
  21. 21. Bash Scripting functions function fixme { echo $0 $1 $2 $3 } Как викаме функция # ./fixme a j k ./fixme a j k #
  22. 22. Bash Scripting Bash regular expressions #!/bin/bash regex=$1 if [[ $1 =~ $regex ]]; then echo “$2 matched” else echo “$2 NOT matched” fi # ./regex search searchstring searchstring matched
  23. 23. Bash Scripting Bash substitutions #!/bin/bash for i in /dir/*; do echo ${i/.*/} done $ ls *.pl *.txt collect-plans.pl cpu-abuse.pl landing.txt ap_write.txt $ ./script collect-plans cpu-abuse landing ap_write
  24. 24. Bash Scripting Bash substitutions Inside ${ ... } Action taken name:number:number Substring starting character, length #name Return the length of the string name#pattern Remove (shortest) front-anchored pattern name##pattern Remove (longest) front-anchored pattern
  25. 25. Bash Scripting Bash substitutions Inside ${ ... } Action taken name%pattern Remove (shortest) rear-anchored pattern name%%pattern Remove (longest) rear-anchored pattern name/pattern/string Replace first occurrence name//pattern/string Replace all occurrences
  26. 26. Bash Scripting Bash substitutions hackman@terion:~$ a='123456' hackman@terion:~$ echo ${a:3:5} 456 hackman@terion:~$ echo ${a:1:4} 2345 hackman@terion:~$ echo ${#a} 6
  27. 27. Bash Scripting Bash substitutions (front) hackman@terion:~$ a='grizzly.yuhu.biz' hackman@terion:~$ echo ${a#*.} yuhu.biz hackman@terion:~$ echo ${a##*.} biz
  28. 28. Bash Scripting Bash substitutions (rear) hackman@terion:~$ a='grizzly.yuhu.biz' hackman@terion:~$ echo ${a%.*} grizzly.yuhu hackman@terion:~$ echo ${a%%.*} grizzly
  29. 29. Bash Scripting Bash substitutions (regexp) hackman@terion:~$ a='pate.pate.patence.txt' hackman@terion:~$ echo ${a/pate/kate} kate.pate.patence.txt hackman@terion:~$ echo ${a//pate/kate} kate.kate.katence.txt
  30. 30. Bash Scripting Bash arrays #!/bin/bash #!/bin/bash a=(x y z) a=(x y z) echo ${a[0]} echo ${#a[*]} echo ${a[1]} # ./array echo ${a[2]} 3 # ./array x y z
  31. 31. Bash Scripting Parameter expansion $ function am() { for i in "$*"; do echo ”$i”; done } $ am jo ji ko “jo ji ko” $ function am() { for i in "$@"; do echo ”$i”; done } $ am jo ji ko “jo” “ji” “ko”
  32. 32. Bash Scripting Bash arrays #!/bin/bash #!/bin/bash a=(x y z) a=(x y z) for i in “${a[*]}”; do for i in “${a[@]}”; do # ./array # ./array x y z x y z
  33. 33. Bash Scripting Bash special variables $0 - the name of the script or shell $# - number of parameters/arguments $! - PID of the last executed background command $? - exit status of the last executed command $* - expands to a single quoted word $@ - expands to separate words $- - current bash flags/options IFS - internal field separator
  34. 34. Bash Scripting Bash arrays hackman@gamelon:~$ ip a l eth0|grep 'inet ' inet 10.2.0.3/24 brd 10.2.0.255 scope global eth0 hackman@gamelon:~$ ip=($(ip a l eth0|grep 'inet ')) hackman@gamelon:~$ echo ${ip[0]} inet hackman@gamelon:~$ echo ${ip[1]} 10.2.0.3/24
  35. 35. Bash Scripting Command execution ip=( $( ip a l eth0|grep 'inet ' ) ) vs. ip=( ` ip a l eth0|grep 'inet ' ` )
  36. 36. Bash Scripting ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Въпроси ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Мариян Маринов(HackMan) mm@yuhu.biz

×