@ OSCON2014
type cd
info man
man man
0
1 to 255
$?
while read var1 var1; do
echo $var1 $var2
done
for name in words; do list; done
for(( expr1 ; expr2: expr3; ))
for (( i=0 ; i<4 ; i++ ))
do
echo $i
done
select name in words; do list; done
[ expression] [[ conditional exp ]]
[[ -n string ]]
[[ -z string ]]
[[ string != string ]]
[[ string =~ regex ]]
[[ string == string ]]
[[ -e string ]]
[[ -d file ]]
[[ -f file ]]
[[ -t string ]]
[[ -n string ]]
[[ -n string ]]
if [[ 'a' == 'a' ]]
then
echo "yes"
else
echo "nope"
fi
case word in
pattern1);
list1:;
patern2 | pattern3);
list;;
esac
case one in
o)
echo "noooo";;
o*)
echo "yeeees";;
*)
echo "anything else";;
esac
subshell (list) groupcommand { list ; } # trailing ; and space for bash parsing group command lets you run a command in current shell /w same vars groping commands to pipe them
$$ PID of ccurrent shell
echo $(</etc/os-release)
$(list)
>(list)
<(list)
$*
$@
$-
$$
$!
%?
$_
${12}
# extraction
${param:offset}
${param:offset:lenght}
# removal from left
${param#pattern}
${param##pattern}
# removal from right
${param%pattern}
${param%%pattern}
indexed [1] => "potato" associative
(( math stuff ))
name++
++name
--name
name--
echo $(( 3+4 ))
# echo the value of eval
echo {10..55..5}%
# use function name and params
words ()
{
for word in "$@";
do
echo "$word";
done
}
$(declare -f xxxxx); xxx
grep -i string filename
# -i for case insensitive
# string manipultaion
man sed
man awk
# cut delimiter filed
cut -d ; -f 4
./myscipt.sh
sudo cp ./myscript /usr/bin
echo is output to stind
a=Hello
b="Hello world!"
echo $a$b
# declaring int
declare -i d=123
declare -r # read-only
declare -l #loweracse
declare -u #uppercase
# built-in
$BASH_VERSION
$MACHTYPE
$0 # scriptname
Storing the value of the command into a varabile
a=$(ping -c 1 8.8.8.8)
a=$(COMMAND)
d=2
e=$((d+d))
((e++))
echo $e
[[ EXPRESSION ]]
-gt -lt -eq -le -ge -ne
Logical op
- [[ $a && $b ]]
- [[ $a || $b ]]
- [[ ! $a ]]
[[ -z $a && -n $b ]]
while read f ; do
echo $f
done < file.txt
#!/bin/bash
[[ -f ./file.txt ]] && touch file.txt
echo "potato">> file.txt
echo "new stuff" >> file.txt
echo "nowhere to be" >> file.txt
echo "rooomba" >> file.txt
# test
i=1
while read f ; do
echo "line $i is: $f"
((i++))
done < file.txt
cat file.txt
exit 0
cat << EndOfTxt
potaoto
motato
roomba
EndOfTxt
# - dash erase leading tabs
cat <<- END
tomato
END
#!/bin/bash
greentxt="\033[32m"
bold="\033[1m"
normal="\033[0m"
logdate=$(date +"%Y%m%d")
logfile="$logdate"_report.log
echo -e $bold"Quick sys report for: "$greentxt"$HOSTNAME"$normal
printf "\tSystemType:\t%s\n" $MACHTYPE
cat <<- EOF > $logfile
Report was successfully created!
Check the report!
EOF
printf "BASH:\t%s\n" $BASH_VERSION >> $logfile
tar -cvf file1.tar file2.txt # compress a file
tar -xvf file1.tar # extract a file
netcat -k -l 9999 >> example_log.txt # listening server
date | nc 192.168.0.4 9999 # send output of command
nc localhost 4444 < /bin/bash #
nmap -sP # ping
nmap -sT # 3-way handshake
nmap -sV # system version
sudo nmap -O
# nmap scripts
# agressive / non agressive scripts
nmap -sC 192.168.0.22
nmap --script "http-*" 192.168.0.34
nmap --script "(ssh and not brute)"
nmap -A # not recommanded, agressive
xz -d someting.img.xz
dd if=smthin.img of=/dev/s bs=4MB status=progress
tar -cvf file1.tar file2.txt # compress a file
tar -xvf file1.tar # extract a file
netcat -k -l 9999 >> example_log.txt # listening server
date | nc 192.168.0.4 9999 # send output of command
nc localhost 4444 < /bin/bash #
nmap -sP # ping
nmap -sT # 3-way handshake
nmap -sV # system version
sudo nmap -O
# nmap scripts
# agressive / non agressive scripts
nmap -sC 192.168.0.22
nmap --script "http-*" 192.168.0.34
nmap --script "(ssh and not brute)"
nmap -A # not recommanded, agressive
xz -d someting.img.xz
dd if=smthin.img of=/dev/s bs=4MB status=progress
tar -cvf file1.tar file2.txt # compress a file
tar -xvf file1.tar # extract a file
netcat -k -l 9999 >> example_log.txt # listening server
date | nc 192.168.0.4 9999 # send output of command
nc localhost 4444 < /bin/bash #
nmap -sP # ping
nmap -sT # 3-way handshake
nmap -sV # system version
sudo nmap -O
# nmap scripts
# agressive / non agressive scripts
nmap -sC 192.168.0.22
nmap --script "http-*" 192.168.0.34
nmap --script "(ssh and not brute)"
nmap -A # not recommanded, agressive
xz -d someting.img.xz
dd if=smthin.img of=/dev/s bs=4MB status=progress