2. 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
3. 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
4. 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
5. 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
6. Users, su, sudo
● su, sudo, visudo, /etc/sudoers, su user
● sudo bash
● /etc/passwd
● ~/
8. 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
9. 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
11. Named Pipes and Network and Signal Processing
● Network
exec 9<>/dev/tcp/google.com/80
echo -e "GET / HTTP/1.0nn" >&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
13. Launchers
● When you drag a file or folder onto a launcher
it is sent as $1 to the launcher script
14. 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 ...