Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bash Shell Scripting Workshop

Bash Shell Scripting Workshop

A little about Bash and what it can do

Avatar for Ahmed Magdy

Ahmed Magdy

May 31, 2012
Tweet

More Decks by Ahmed Magdy

Other Decks in Programming

Transcript

  1. File System • / is not allowed in a file

    name • file extensions are not important • File and folder names are case sensitive • Names containing spaces are escaped by \ or "" • Hidden files start with . • Current directory alias . and parent directory alias .. • ls, ls -a, ls -l, cp, mv, rm, rm -rdf, ln, ln -sf
  2. Variable and Strings • Strings are enclosed in “” ''

    or just written with spaces escaped by \ VAR=”How are you” VAR='How are you' VAR=HOW\ are\ you • Variables $VAR ${VAR} # e.g. ${FILE}.txt echo “my name is $NAME” echo “my name is “$NAME read VAR read VAR1 VAR2
  3. Math • echo $((1+1)) • echo $[1+1] • echo `expr

    1 + 1` • A=$((B*C)) • let A+=1 • echo "3.4+7/8-(5.94*3.14)" | bc
  4. File Ownership and Permissions • Group and Ownership chown, chgrp,

    ls -l chown user:group file sudo chown • Permissions of files and directories chmod 755 file chmod +x file chmod -x file (r)ead (w)rite e(x)ecute 4 2 1
  5. Processes and Subshells • ps, pgrep, top, jobs, fg, &,

    kill, signals • Sub-shells from : • Loops • $() • Back ticks `` • bash command • /proc • fuser
  6. Loops • While loop while read VAR; do echo $VAR;

    done • For each loop for VAR in {1..5}; do echo $VAR; done for VAR in 1 2 3 4 5; do echo $VAR; done for VAR in {0..10..2}; do echo $VAR; done for VAR in $(seq 0 2 10); do echo $VAR; done • For loop with counter for (( i=0; i<=10; i+=2 )); do echo $i; done
  7. if and switch case • If then, else, elif, fi

    if [ -d $F ]; then rm -rdf $F elif [ -f $F ]; then rm $F else echo “Unknown file type” fi • case, esac while read l; do case $l in 1) echo "One";; 2) echo "Two";; 3) echo "Three";; *) echo "Invalid"; break;; esac done
  8. I/O Redirection • >, >>, <, &>, 2&>, 2>, |

    echo “error” 1>&2 # redirect std_output to std_err some_error_producing_command 2>&1 some_command &>> log_file_errors_and_output some_command >> log_file_output_only some_command 2>> log_file_errors_only command | filter_command command < file command > file # truncates and writes into file command1 2>&1 | command2 command1 |& command2 • Mute by redirecting to /dev/null file
  9. Named Pipes and Network and Signal Processing • Network exec

    9<>/dev/tcp/google.com/80 echo -e "GET / HTTP/1.0\n\n" >&9 while read line <&9; do echo $line; done • Named Pipes mkfifo pipe while read l; do echo $l; done < pipe echo “hi” > pipe # on another terminal window • Signal Processing #!/bin/bash function handle_signal () { echo "Hi man I got the signal" } trap "handle_signal; exit" SIGINT sleep 100
  10. Cool Stuff and GUI • Install these: libnotify-bin, zenity, festival

    • date '+%l oclock' | festival –tts • zenity --list --hide-header --text="Are you coming today?" --checklist --column "" --column "" FALSE "Yes" FALSE "No" FALSE "May be" • zenity --calendar --date-format="%Y/%m/%d" • zenity --text="Select Hour" --title="Select Hour" --scale --min-value=0 --max- value=24 --value=17 • zenity --text="Select Minutes" --title="Select Minutes" --scale --min-value=0 --max-value=59 --value=0 • zenity --info --text="Did you know this info" • zenity --error --text="There is an error" • zenity --entry | festival --tts • zenity --file-selection • <progres command> | zenity --progress --auto-close • notify-send -t 10 "hi man" "details"
  11. Launchers • When you drag a file or folder onto

    a launcher it is sent as $1 to the launcher script
  12. Example Codes • Bluetooth Obex-push NEW_FILE_NAME=`echo $1 | awk -vFS="/"

    '{print $NF;}'` ussp-push 00:22:65:89:22:E6@9 "$1" "$NEW_FILE_NAME" • Close the computer when your browser finishes downloading your_file while true; do sleep 5; if [ -z "`fuser ~/your_file`" ]; then break; fi; done; sudo init 0 • Tell me the time whenever I lock my screen or unlock it • Simple spider using curl and wget • Log your time easily and report to a Redmine service • Think of more ...