视频地址:https://www.bilibili.com/video/av23774844/?p=12
typeset
#!/bin/bash
function f1 {
typeset x
x=7
y=8
}
x=1
y=2
echo x is $x
echo y is $y
f1
echo x is $x
echo y is $y
declare
#!/bin/bash
declare -l lstring="ABCdef"
declare -u ustring="ABCdef"
declare -r readonly="A Value"
declare -a Myarray
declare -A Myarray2
echo lstring = $lstring
echo ustring = $ustring
echo readonly = $readonly
readonly="New Value"
Myarray[2]="Second Value"
echo 'Myarray[2]= ' ${Myarray[2]}
Myarray2["hotdog"]="baseball"
echo 'Myarray2[hotdog]= ' ${Myarray2["hotdog"]}
read
while
for
while.sh
#!/bin/bash
x=1
while
((x<10))
do
echo loop $x
date >date.$x
((x=x+1))
done
whie2.sh
#!/bin/bash
ls -l /etc | while
read a b c d
do
echo owner is $c
done
利用这种方法就可以整理出文件的拥有者
for.sh
#!/bin/bash
for i in dog cat hotdog
do
echo i is $i
done
for i in `seq 3 5`
do
echo i in seq is $i
done
for i in {N..P}
do
echo i in letter list is $i
done
for d in $(<data_file)
do
echo d in data_file is $d
done
for f in $(find /etc 2>/dev/null | grep grub)
do
echo grub named things are $f
done
function
func.sh
#!/bin/bash
function myfunc {
echo starting myfunc
return
echo this will not be executed
}
myfunc
n=$(myfunc)
echo n is $n
func2.sh
#!/bin/bash
function f2 {
echo in f2
exit 2
echo more in f2
}
echo strarting
f2
echo after f2
视频地址:https://www.bilibili.com/video/av23774844/?p=15
重定向和管道
#!/bin/bash
echo Just '>' -----------------------------
find /etc -name grub >grub.out
echo Doing '2>' -----------------------------
find /etc -name grub 2>errs.out
echo Doing '&>' -----------------------------
find /etc -name grub &>both.out
#!/bin/bash
exec 19<data_file
lsof -p $$
cat 0<&19
exec 7>&1 #save stdout in 7
exec 7>&- # close stdout
lsof -p $$
cat # no stdout now
exec 1>&7 # copy 7 back to stdout
cat
case
#!/bin/bash
echo -n "Print message? "
valid=0
while
[ $valid == 0 ]
do
read ans
case $ans in
yes|YES|y|Y ) echo Will print the message
echo The Message
valid=1
;;
[nN][oN] ) echo Will Not print the message
valid=1 ;;
* ) echo Yes or No of some form please ;;
esac
done
if
#!/bin/bash
if test -x /bin/ls
then
if
[ ! -w /etc/hosts ]
then
if
echo about to look for foobar
grep -q foobar /etc/passwd
then
echo foobar found in /etc/passwd
else
echo foobar not found
fi
fi
else
echo Oh no, /bin/ls not excutable
fi
test
#!/bin/bash
x=01
y=1
echo comparing $x and $y
if
[ $x == $y ]
then
echo ==
else
echo not ==
fi
if
[ $x -eq $y ]
then
echo eq
else
echo not eq
fi
if(( x==y ))
then
echo '(())' ==
else
echo not '(())' ==
fi
注意!
== 比较的是字符串
eq 比较的是数字
算术运算
#!/bin/bash
((n=2**3 + 5)) # 2的3次方
echo n = $n # 13
((y=n^4)) # n(13)异或4
echo y = $y # 9