Python自動化メンテナンスのshell究極ガイド
17938 ワード
1 shell
1.1 &shell[ ]
1.1.1
&shell
: --> ( + )
:
shell
1.1.2 shell
shell
shell
shell
windows cmd.exe
linux bash
:
echo $SHELL shell
cat /etc/shells shell
,
[root@sswang ~]# echo $SHELL
/bin/bash
[root@sswang ~]# cat /etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
[root@sswang ~]# mkdir /data/scripts -p
[root@sswang ~]# cd /data/scripts/
[root@sswang scripts]# ls
[root@sswang scripts]# vim test.sh
[root@sswang scripts]# cat test.sh
#!/bin/bash
#
echo $SHELL
cat /etc/shells
[root@sswang scripts]# /bin/bash test.sh
/bin/bash
/bin/sh
/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin
1.2 shell [ ]
1.2.1
vim
:
:
#
:
#
:<
第2章核心知識[応用]2.1式2.1.1試験文応用場面判断条件が成立するか否か
test
[ ]
:
[]
:
test 1 = 1
echo $?
test 1 = 2
echo $?
test 1 = 1
echo $?
test 1 = 2
echo $?
[ 1 = 1 ]
echo $?
[ 1 = 2 ]
echo $?
[ 1 = 3 ]
echo ?$
[ 1 = 3 ]
echo $?
[ 1 = 3]
[ 1 = 3] ]
echo $?
2.1.2
&& , , ,
|| , , ,
:
[root@shell scripts]# [ 1 = 2 ] && echo " "
[root@shell scripts]# [ 1 = 2 ] || echo " "
[root@shell scripts]# [ 1 = 1 ] && echo " "
[root@shell scripts]# [ 1 = 1 ] || echo " "
[root@shell scripts]#
-f
-d
-x
:
[ -f get_name1.sh ] && cat get_name1.sh
[ -f get_name2.sh ] && cat get_name1.sh
ls
[ -d backup ] && mv *.sh backup
ls
[ -d backup ] || mkdir backup
ls
[ -x get_name1.sh ] && ./get_name1.sh
[ -x get_name1.sh ] || chmod +x get_name1.sh
ll
-gt -lt -eq -ne -le -ge
:
,ne eq
:
[ 2 -eq 1 ]
echo $?
[ 2 -eq 2 ]
echo $?
[ 2 -ne 1 ]
echo $?
[ 2 -ne 2 ]
echo $?
[ 2 -lt 21 ]
echo $?
[ 2 -gt 21 ]
echo $?
==
!=
:
:
[ aaa == bbb ]
echo $?
[ aaa != bbb ]
echo $?
a=abcd
b=defg
[ $a == $b ]
echo $?
[ $a != $b ]
echo $?
2.1.3
:
$(( ))
let
:
let , ,
echo $((1 + 3))
echo $((1 + 4))
echo $((1 + 44))
echo $((1 + 44e))
a=3
echo $((a*3))
echo $((a*4))
a=$((a+4))
echo $a
a=$((a+4))
echo $a
i=4
let j=3+4
echo $j
let j=i+5
echo $j
let j = i +5
:
:bc
2.1.4
:
array_name=(value1 ... valuen)
:
()
array_name[0]=value0
echo ${array_name[index]}
echo ${!array_name[index]} @ *
echo ${#array_name[index]}
echo ${#array_name[@|#]}
:
array_name=(value0 value1 value2 value3)
echo ${array_name[1]}
echo ${array_name[2]}
echo ${array_name[@]}
echo ${array_name[*]}
echo ${!array_name[@]}
echo ${!array_name[2]}
echo ${!array_name[value1]}
echo ${!array_name[value0]}
${#array_name[index]}
${#array_name[2]}
echo ${#array_name[2]}
echo ${!array_name[*]}
echo ${#array_name[*]}
echo ${#array_name[1]}
unset array_name
array1=(a b c d)
array2=( b d e f g )
echo ${array1[0]}
echo ${array1[2]}
echo ${array2[2]}
echo ${array2[@]}
echo ${array2[*]}
echo ${!array1[@]}
echo ${#array1[@]}
echo ${#array2[@]}
echo ${#array2[1]}
array3[2]=nihao
array3[1]=dd
echo ${#array3[@]}
echo ${#array3[2]}
echo ${array3[2]}
array3[12]=dd
echo ${array3[12]}
array_name[index]=value
:
echo ${array3[index]}
echo ${array3[index]: : }
array_name[index]=value
${array3[index]/ / }
: ,
:
unset array_name[index]
unset array_name
:
array3[12]=dddksafjldsajfklsaj
echo ${array3[12]}
echo ${array3[12]:9:4}
array3[12]=gaile
echo ${array3[12]}
echo ${array3[12]/ai/haha}
echo ${array3[12]}
unset array3[12]
echo ${array3[12]}
echo $#{array3[@}
echo ${#array3[@}
echo ${#array1[]}
echo ${#array2[]}
echo ${#array2[@]}
unset array2
echo ${#array2[@]}
2.2 linux
2.2.1
:
;
>
>>
:
echo nihao > file.txt
ls
cat file.txt
echo nihao1 > file.txt
cat file.txt
echo nihao2 >> file.txt
cat file.txt
echo nihao2 >> file.txt
cat file.txt
2.2.2 |
:
:
:
ls | grep test
env | grep SHELL
2.2.3
&
2>&1
/dev/null
:
sleep 6 &
ls
sleep 16 &
ps aux | grep sleep
ls
echo $?
kdsafljdsklajflsd
echo $?
echo $SHELL
echo $SHELL 1 >> zhengque
cat zhushi.sh
cat zhengque
echo $SHELL 1>> ok
cat ok
jdklsafj 2>> nook
cat nook
ls
vim cuowu.sh
cat cuowu.sh
/bin/bash cuowu.sh
/bin/bash cuowu.sh 1>> ceshi-ok 2>> ceshi-nook
cat ceshi-ok
cat ceshi-nook
/bin/bash cuowu.sh >> ceshi-all 2>&1
cat ceshi-all
ls /dev/null
ls /dev/zero
ls >> /dev/null
/bin/bash cuowu.sh >> /dev/null 2>&1
python manage.py runserver >> /dev/null 2>&1 &
2.3
2.3.1 if
if [ ]
then
fi
:
:
1
2
:
#!/bin/bash
# if
if [ $1 == "nan" ]
then
echo " : "
fi
if [ ]
then
1
else
2
fi
:
, 1
, 2
:
#!/bin/bash
# if
if [ $1 == "nan" ]
then
echo " : "
else
echo " : "
fi
if [ ]
then
1
elif [ 2 ]
then
2
else
3
fi
:
if , elif-then
:n n+1
:
#!/bin/bash
# if
if [ "$1" == "nan" ]
then
echo " "
elif [ "$1" == "nv" ]
then
echo " "
else
echo " , "
fi
#!/bin/bash
# if
arg="$1"
if [ "${arg}" == "start" ]
then
echo " ..."
elif [ "${arg}" == "stop" ]
then
echo " ..."
elif [ "${arg}" == "restart" ]
then
echo " ..."
else
echo " $0 : /bin/bash $0 [ start|stop|restart ]"
fi
2.3.2 case
:$
case in
1)
1
;;
...
n)
n
;;
esac
:
1 : case-esac
2 : )
3 : ;;
:
#!/bin/bash
# case
arg="$1"
case "${arg}" in
"start")
echo " ..."
;;
"stop")
echo " ..."
;;
"restart")
echo " ..."
;;
*)
echo " $0 : /bin/bash $0 [ start|stop|restart ]"
;;
esac
:
*
*
case linux
2.3.3
for
:
:$
for in
do
done
:
#!/bin/bash
# for
for i in $(ls /data/scripts)
do
echo "/data/scripts ${i}"
done
while
: ,
: ,
:$
while
do
done
:
#!/bin/bash
# while
a=1
while [ ${a} -lt 5 ]
do
echo "${a}"
let a=a+1
sleep 1
done
until
: ,
: ,
:
until
do
done
:
#!/bin/bash
# until
a=1
until [ ${a} -eq 5 ]
do
echo "${a}"
a=$((a+1))
sleep 0.5
done
2.3.4
: , ,
break
#!/bin/bash
# break
# , ,
while :
do
echo -n " , 1~5 :"
read anum
case "${anum}" in
1|2|3|4|5)
echo " ${anum}"
;;
*)
echo " , "
break
;;
esac
done
break n
#!/bin/bash
#
for var1 in {1..5}
do
for var2 in {0..4}
do
if [ $var1 -eq 3 -a $var2 -eq 2 ]
then
break 2
else
echo "$var1 $var2"
fi
done
done
continue
: ,
#!/bin/bash
# continue
# , ,
while :
do
echo -n " , 1~5 :"
read anum
case "${anum}" in
1|2|3|4|5)
echo " ${anum}"
;;
*)
continue
echo " , "
;;
esac
done
exit
: , , , 。
1 -- break 1
#!/bin/bash
#
for var1 in {1..5}
do
for var2 in {0..4}
do
if [ $var1 -eq 3 -a $var2 -eq 2 ]
then
break 1
else
echo "$var1 $var2"
fi
done
done
2 -- exit
#!/bin/bash
#
for var1 in {1..5}
do
for var2 in {0..4}
do
if [ $var1 -eq 3 -a $var2 -eq 2 ]
then
exit
else
echo "$var1 $var2"
fi
done
done
2.4
2.4.1
:
(){
}
:
:
,
。
:
#!/bin/bash
#
#
dayin(){
echo " , aaa"
}
#
dayin
:
(){
$n
}
:
:
1
2 ,
3 ,
:
[root@shell scripts]# cat arg-func.sh
#!/bin/bash
#
#
dayin(){
xingming="$1"
echo " , ${xingming}"
}
#
dayin aaa
dayin bbb
dayin ccc
[root@shell scripts]# /bin/bash arg-func.sh
, aaa
, bbb
, ccc
/bin/bash
:
(){
$1
}
$1
:
,
:
#!/bin/bash
#
#
dayin(){
xingming="$1"
echo " , ${xingming}"
}
#
dayin $1
( )
:
, , ,
--
:
#!/bin/bash
# --
#
ARG="$1"
#
dayin(){
xingming="$1"
echo " , ${xingming}"
}
#
dayin "${ARG}"
2.4.2
2.5
2.5.1 grep
grep [ ] [ ]
-c
-n
-v
:
grep -c aaa grep.txt
grep -c nihao grep.txt
grep -cn nihao grep.txt
grep -n nihao grep.txt
grep -v nihao grep.txt
grep -vn nihao grep.txt
:
-r
-i
egrep
:
grep -nr aaa /data/*
grep -nr AAA /data/*
grep -nri AAA /data/*
cd /data/scripts/
ls
cat grep.txt
egrep 'AAA|bbb' grep.txt
egrep -n 'AAA|bbb' grep.txt
egrep -n '^N' grep.txt
2.5.2 sed
sed [ ] ' [ ]' [ ]
:
-n
-i
:
: / # @ !
:
p
s
: sed ' p'
sed '/ /p'
sed '3p' sed.txt
sed -n '3p' sed.txt
sed -n '2,3p' sed.txt
sed -n '/SED4/p' sed.txt
:
sed -i ' s# # # ' [ ]
sed -i 's# # #g' [ ]
;
,
,
g,
:
sed 's#sed#SED#' sed.txt
cat sed.txt
sed -i 's#sed#SED#' sed.txt
cat sed.txt
sed -i '2s#sed#SED#2' sed.txt
cat sed.txt
sed -i 's#sed#SED#g' sed.txt
cat sed.txt
sed ' a\ '
sed '/ /a\ '
:
cat -n sed.txt
sed -i '2a\add-1' sed.txt
cat -n sed.txt
sed -i '1,3a\add-3' sed.txt
cat -n sed.txt
sed -i '/SED1/a\add-sed1' sed.txt
cat -n sed.txt
sed ' i\ '
sed '/ /i\ '
:
cat -n sed.txt
sed -i '1i\insert-1' sed.txt
cat -n sed.txt
sed -i '6,8i\insert-1' sed.txt
cat -n sed.txt
sed -i '/SED5/i\insert-sed5' sed.txt
cat -n sed.txt
sed ' d'
sed '/ /d'
:
cat -n sed.txt
sed -i '8d' sed.txt
cat -n sed.txt
sed -i '7,11d' sed.txt
cat -n sed.txt
sed -i '/add-3/d' sed.txt
cat -n sed.txt
sed -i '/SED/d' sed.txt
cat -n sed.txt
2.5.3 awk
:
awk [ ] '[ ]' [ ]
:
'{print $n}'
$0
$n
,
'/ /'
:
NR
awk '{print }' awk.txt
awk '{print $0 }' awk.txt
awk '{print $1 }' awk.txt
awk '{print $3 }' awk.txt
awk '{print $1,$3 }' awk.txt
awk '{print $1$3 }' awk.txt
awk '{print $4$2 }' awk.txt
awk '{print $4,$2 }' awk.txt
awk '{print $4,$2 }' awk.txt
awk '{print NR,$4,$2 }' awk.txt
awk 'NR==1 {print NR,$4,$2 }' awk.txt
awk '/awk/' awk.txt
-F
-f
-v awk
awk -F ':' '{print $1}' passwd.txt
awk -F ':' '{print $1,$7}' passwd.txt
cat passwd.txt
vim filename
cat filename
awk -f filename passwd.txt
vim filename
cat filename
awk -f filename passwd.txt
awk -v vari=100 '{print vari}'
echo | awk -v vari=100 '{print vari}'
echo | awk '{print v1,v2}' v1=1000,v3=300
echo | awk '{print v1,v2}' v1=1000,v2=300
echo | awk '{print v1,v2}' v1=1000 v2=300
BEGIN{ } ,
/pattern/{ } 、
END{ } ,
:
awk -F ':' '{print $1,$7}' passwd.txt
awk -F ':' 'BEGIN{OFS='|'} {print $1,$7}' passwd.txt
awk -F ':' 'BEGIN{OFS="|"} {print $1,$7}' passwd.txt
awk -F ':' 'BEGIN{print " bash"} {print NR,$1,$7} END{print "--- ----"} ' passwd.txt
if :
: '{ if print }'
: (( 1 && 2 ))
:
ls -l | awk '{ if (($5>=200)) print " : " $9 "\t" " :" $5 "B" }'
ls -l | awk '{ if (($5>=200 && /^-/)) print " : " $9 "\t" " :" $5 "B" }'
for :
:'{ for print }'
: for( )
:
echo 'abcdefg' | awk -F '' '{ for(i=1;i<=NF;i++) print $i}'
echo 'abcdefg' | awk -F '' '{ for(i=NF;i>=1;i--) print $i}'
2.5.4 find
find [ ] [ ] [ ] [ ]
:
-name
-type
-mtime
-path
-prune
:
-print ,
-ls
-exec \;
:
{}
| xargs
, 。
find / -name "ok"
find /data -name "ok"
find /data -name "o*"
find /data -type f
find /data -type d
find /data -type f -name "*fu*"
ls
stat ok
find /data -mtime -5
find /data -mtime +5
find /tmp -mtime +5
stat /tmp/.Test-unix
find /tmp -mtime +5 -print
ls
find /data/scripts -path "/data/scripts/backup" -prune
find /data/scripts -path "/data/scripts/backup" -prune -o -print
ls
mkdir backup2
mv ok nook backup2/
ls
find . \( -path ./backup -o -path ./backup2 \) -prune -o -print
find . -perm 640
find . -perm 640 -print
find . -perm 640 -ls
find . -perm 640 -exec mv {} {}.bak \;
find . -perm 640 -ls
ls
find . -name "passwd*" -ls
find . -name "passwd*" -exec cp {} {}.bak \;
ls
find . -name "*txt.bak" -exec cp {} {}.bak \;
find . -name "*txt.bak"
find . -name "*txt.bak" -exec rm -f {} \;
ls
find . -name "*bak.bak"
find . -name "*bak.bak" | xargs rm -f
find . -name "*bak.bak"
:
-exec
xargs