本帖涵盖了Bash的课程练习和所有演示过的命令脚本,可以对照着0908的Shell直播课程回放视频来练习。
Android和iOS的Shell应用放在后面的UI自动化和专项测试课程中去介绍。
有问题可以在本帖下留言,后续会有老师来答复。
Bash课堂笔记&练习
1、if.sh
#!/bin/bash
if [ 1 -eq 1 ];then
\tls
else
\techo hello
fi
if [ 2 -lt 3 ];then
\tls
elif [ 3 -gt 2 ];then
\techo "3>2"
fi
2、for.sh
#!/bin/bash
for ((i=1;i<=10;i++))
do
\techo $i
done
a=(a b c d e)
set -x
for var in ${a[@]}
do
\techo $var
done
set +x
3、while.sh
i=1
while [ $i -le 10 ];do
\techo $i
\t((i=i+1))
\t[ $i -eq 5 ] && continue
done
pwd
echo $$
4、argv.sh
#!/bin/bash
# tmm 180908 16 author myname
echo $0
echo $1
echo $2
echo '$9: '$9
echo '$10: '$10
echo "-----"
echo $@
echo $*
echo "------"
echo ${#*}
echo ${#@}
5、function.sh
#!/bin/bash
function argv()
{
echo $0
echo $1
echo $2
echo '$9: '$9
echo '$10: '$10
echo "-----"
echo $@
echo $*
echo "------"
echo ${#*}
echo ${#@}
shift 1
echo "------"
echo $9
}
argv 1 2 3 4 5 6 7 8 9 10
6、awk.sh
#!/bin/bash
echo $PATH |awk 'BEGIN{RS=":"}{print $0}' | awk -F '/' '{print $1 $2 $3 $4}'
function awktest(){
\tls -l |while read line
\tdo
\t\ta=$line
\t\tdirfilename=`echo $a|awk '{print $9}'`
\t\techo $dirfilename
\tdone
}
awktest
写一个脚本:
获取系统逻辑CPU个数
获取当前系统的5分钟负载
判断当前负载 高低
输出 负载高?低
输出 获取系统逻辑CPU个数 当前系统的5分钟负载
7、testaverage.sh
#!/bin/bash
cpuprocessornum=`grep processor /proc/cpuinfo|wc -l`
average5=`w|head -1|awk -F 'load' '{print $2}'|awk '{print $2}'|sed 's/,//g'`
if [ `echo "$cpuprocessornum<=$average5"|bc` -eq 1 ];then
\techo "系统负载高"
else
\techo "系统负载低"
fi
echo "cpuprocessornum: "$cpuprocessornum
echo "average5: "$average5
Shell课上用到的所有命令(和回放视频对应)
1 ls
2 who
3 df -h
4 top
5 free -m
6 cat /proc/cpuinfo
7 ls
8 pwd
9 ls
10 ls -a
12 ls .
13 ls ..
14 pwd
15 cd ..
16 pwd
17 ls
18 cd
19 ls
20 ls -a
21 ls -al
22 ls -alR
23 mkdir dir
24 mkdir -p dir/dir1
25 ls -alR
26 ls -alt
27 ls -al
28 ls -alt
29 ls -altr
30
44 cd -
45 cd ~
64 cd /root
65 pwd
66 ls
67 mkdir dir3
68 ls
69 ls -l dir3/
70 mkdir dir3/dir4
71 ls
72 mkdir dir5/dir6
73 mkdir -p dir5/dir6
74 ls -l
75 ls -l dir5
76 ls
77 mkdir dir6 dir7
79 touch file1
80 ls
81 ls -l
82 vi file2
83 ls
84 echo "hello"
85 echo "hello" > file3
86 ls
87 cat file3
88 echo "hello" >> file4
89 ls -l
90 cat file4
91 vi file2
92 ls
93 history
94 vi file1
95 ls
96 cp file1 file5
97 ls -l
98 cp dir dir8
99 cp -r dir dir8
100 ls -l
101 ls
102 cp file1 file2
110 cp -rfp file1 file2
111 ls
112 cp -rfp dir2 dir3
113 move
114 ls
115 mv file1 file6
116 ls
117 mv dir dir9
118 rename
119 ls
120 history
121 ls
122 cp file1 file2
123 cp file2 file3
124 cp dir2 dir3
125 cp -r dir2 dir3
126 ls
145 ls -l /etc/profile
146 vi /etc/profile
162 more .bash_history
163 more /etc/profile
169 wc /etc/profile
170 head -10 /etc/profile
171 tail -10 /etc/profile
172 cat /etc/profile
173 tail -n 100 /var/log/messages
174 tail -n 100 /etc/profile
182 head -30 /etc/profile | tail -10
193 head -40 /etc/profile | tail -11
195 wc -l file4
203 stat file4
212 top
213 ps
214 who
215 ps axf
216 ps auxf
217 ps a
219 ps -A
220 ps
221 ps x
222 ps ax
223 ps axf
224 ls
225 ps axuf
226 ps axf
227 ps auf
232 date
233 ifconfig
234 cat /proc/cpuinfo
235 cat /proc/cpuinfo |grep "physical id"
236 cat /proc/cpuinfo |grep "process"
239 free -m
240 free
244 df -h
246 cat /proc/cpuinfo
250 sleep 10 &
251 jobs
265 wtmp
266 last
267 last|awk '{print $3}'
268 last|awk '{print $3}'|uniq -n
269 last|awk '{print $3}'|sort
270 last|awk '{print $3}'|sort|uniq -n
271 last|awk '{print $3}'|sort|uniq c
272 last|awk '{print $3}'|sort|uniq -c
273 last|awk '{print $3}'|sort|uniq -u
274 last|awk '{print $3}'|sort|uniq -c
276 a=1
277 echo $a
278 a = 1
279 a=1
280 a=hello
281 echo $a
282 a=hello testerhome'
283 a=hello testerhome
284 a="hello testerhome"
285 echo $a
286 a='hello testerhome'
287 echo $a
288 echo "$a"
289 echo '$a'
290 ls
291 ls -l
292 ls
293 a=`ls`
294 echo $a
295 ls
296 s
297 ls
298 echo $a
299 ls
300 echo $a_a
301 echo ${a}_a
302 echo "$a"
303 echo "$a"_a
304 echo "$a" ${a}
305 ls
306 a=1
307 echo $a
308 ls
309 ping baidu.com
310 netstat -nltp
311 nslookup
312 w
313 top
314 [[ "xxx" == x* ]] && echo $?
315 [[ "xxx" == x? ]] && echo $?
316 [[ "xxx" == x? ]] echo $?
317 [[ "xxx" == x? ]]
318 echo $?
319 [[ "xxx" == x?? ]]
320 ((a+1))
321 echo ((a+1))
322 ((c=1+1))
323 echo $c
324 ls
325 w
326 echo $PWD
327 pwd
328 echo $USER
329 echo $HOME
330 echo $PATH
334 which cd
337 cat .bashrc
338 ls
339 array=(1 2 34 )
340 echo ${array[@]}
341 echo ${array[*]}
342 echo ${array[0]}
343 echo ${array[2]}
344 echo ${#array[*]}
345 echo ${array[*]}
346 echo ${#array[*]}
347 array=(`ls`)
348 echo ${array[*]}
349 ls
350 array=(ls)
351 echo ${array[*]}
352 array=(`ls`)
353 echo ${array[*]}
354 a=(`ls`)
355 echo $a
356 ls
357 echo $a
358 echo ${a[*]}
359 echo "hhe ss"
360 a=1
361 echo '$a'
362 echo "$a"
363 echo '$a'
364 echo '\\$a'
365 echo $(ls)
366 $(ls)
367 dir4
368 dir5
369 ls
370 a=`ls`
371 echo $a
372 ls
373 echo $((1=1))
374 echo $((1+1))
375 echo $((4*4))
376 a=5
377 echo $((a*4))
378 echo ((1+1))
379 echo ((a=1+1))
380 ((a=1+1))
381 echo $a
382 ({1 10})
383 ({1..10})
384 echo ({1..10})
385 echo ({1 10})
386 echo $({1 10})
387 ls
388 ({1..10})
389 a=({1..10})
390 echo $a
391 a=({1..10})
392 echo ${a[@]}
393 b=(seq 1 10)
394 echo ${b[@]}
395 b=$(seq 1 10)
396 echo ${b[@]}
397 seq 1 10
398 'ee e'
399 a='ee e'
400 `ls`
401 a=`ls`
402 ls
403 a=1
404 echo $((a+1))
405 a=1
406 echo $a
407 echo $((a+5))
408 echo $((a-5))
409 b=4
410 echo $((a*5))
411 echo $((b*5))
412 echo $((b/5))
413 echo $((1.9))
414 echo $((2.9-1))
415 echo $((2/3))
416 echo $((3/2))
417 ((a=3+4))
418 echo $a
421 awk 'BEGIN{print 1/2}'
422 awk '{print 1/2}'
423 awk 'BEGIN{print 1/2}'
425 var="hello testerhome"
426 echo ${#var}
427 echo ${s#hello}
428 echo ${s:1:6}
429 echo ${var:1:6}
430 echo ${var:0:4}
431 echo ${var:0:5}
432 echo ${var:6}
433 echo ${var/hello/welcome/}
434 echo ${var/hello/welcome}
435 ls
436 var=""hello testerhome
437 var=""hello testerhome"
438 var="hello testerhome"
439 echo ${var:6}
440 echo ${var:6:100}
441 echo ${var:6:10}
442 echo ${var:6:7}
443 echo ${var:6:9}
444 echo ${var/hello/welcome/}
445 echo ${var/hello/welcome}
447 echo $?
451 [ -f file4 ] && echo $?
452 [ -f file4 ] || echo $?
453 [ -d file4 ] && echo $?
454 ls
455 [ 1 -eq 2 ] && echo $?
456 [ 1 -eq 2 ]
457 echo $?
458 [ 1 -eq 1 ]
459 echo $?
460 ls
461 [ aa = b ]
462 echo $?
463 [ aa != b ]
464 echo $?
465 [ -n $a ] && echo $?
466 echo $a
467 ls -l
468 ls -l dir*
469 ls dir*
470 ls
471 [[ "xxx" == x* ]]
472 echo $?
473 [[ "xxx" == x? ]]
474 echo $?
475 [[ "xxx" == x?? ]]
476 echo $?
477 ls
478 [ 1 -eq 1 ] && echo $?
479 [ 2 -eq 3 && 1 -eq 1] && echo $?
480 [ 2 -eq 3 && 1 -eq 1 ] && echo $?
481 [[ 2 -eq 3 && 1 -eq 1]] && echo $?
482 [[ 2 -eq 3 && 1 -eq 1]]
483 [[ 2 -eq 3 || 1 -eq 1 ]]
484 echo $?
485 [ $a -eq 3 -a 2 -ne 4 ] && echo $?
486 [ $a -eq 3 -a 2 -ne 4 ] || echo $?
487 ls
488 [ -f file4 ] && echo $?
489 [ -f file3 ] && echo $?
490 ls
491 [ -e file4 ] && echo $?
492 [ -e dir5 ] && echo $?
493 ls
494 cat dir5
495 vi dir5/
496 ls
497 [ -f file4 ]
498 -f file4
499 ls
500 if [];then ls ; fi
501 if [ 1 -eq 1];then ls ; fi
502 if [ 1 -eq 1 ];then ls ; fi
503 if [ 1 -eq 2 ];then ls ; fi
504 if [ 1 -eq 2 ];then ls ;else ls; fi
505 ls
506 if [ 1 -eq 2 ];then ls ;else ls;elif [ 1 -lt 2 ];then echo "hello"; fi
507 if [ 1 -eq 2 ];then ls ;else ls;elif[ 1 -lt 2 ];then echo "hello"; fi
508 if [ 1 -eq 2 ];then ls ;else ls;elif[ 1 -lt 2 ];then; echo "hello"; fi
509 if [ 1 -eq 2 ];then ls ;else ls;elif[ 1 -lt 2 ]; fi
510 if [ 1 -eq 2 ];then ls ;else ls; fi
511 ls
512 if [ 1 -eq 2 ];then ls ;elif[ 1 -lt 2 ];echo fi
513 if [ 1 -eq 2 ];then ls ;elif[ 1 -lt 2 ];then echo 1; fi
514 if [ 1 -eq 2 ];then ls; elif [1 -lt 2 ];then echo 1; fi
515 if [ 1 -eq 2 ];then ls; elif [ 1 -lt 2 ];then echo 1; fi
516 ls
517 if [ 1 -eq 1];then ls ;fi
518 if [ 1 -eq 1 ];then ls ;fi
519 ls
520 if [ 1 -eq 1 ];then ls ;fi
521 vi if.sh
522 bash if.sh
523 ls
524 sh if.sh
525 vi if.sh
526 bash if.sh
527 ls
528 cat if.sh
529 history
530 cat if.sh
531 ls
532 [ 1 -eq 1 ] && echo $?
533 [ 1 -eq 1 ] && echo hello
535 ((i=1+2))
536 echo $i
537 vi for.sh
541 bash for.sh
545 ls -l file*
546 ls file*
547 a=`ls file*`
548 echo ${a[@]}
549 echo $a
550 a=`ls file*`
551 cat for.sh
590 ps axf|grep while
591 bash while.sh &
592 echo $!
593 ps -p 19617
594 ps axf|grep while
595 kill 19617
596 echo $!
597 ps -p 19617
598 ls
599 vi while.sh
600 bash while.sh
601 ps -p 19716
602 echo $PWD
603 echo $PATH
604 vi .bashrc
605 cat .bashrc
606 vi .bashrc
607 source .bashrc
608 ls -l
609 ls -l|awk '{print NR}'
610 ls -l|awk '{print NF}'
611 ls -l|awk '{print RS}'
612 cat /etc/passwd
613 echo $PATH
614 echo $PATH | awk 'BEGIN{RS=":"}{print $0}'
615 echo $PATH | awk 'BEGIN{RS=":"}{print $1}'
616 echo $PATH | awk 'BEGIN{RS=":"}{print $0}' | sed -n '/^\\/bin/,/sbin/p'
627 echo $PATH | awk 'BEGIN{RS=":"}{print $0}'
628 echo $PATH | awk 'BEGIN{RS=":"}{print $0}' |sed -n '/usr/,/\\/home/'
629 echo $PATH | awk 'BEGIN{RS=":"}{print $0}' |sed -n '/usr/,/\\/home/p'
630 echo $PATH | awk 'BEGIN{RS=":"}{print $0}' |sed -n '/usr/,/^\\/home/p'
631 ls
632 cat if.sh
633 cat for.sh
634 cat while.sh
635 bash while.sh
636 ls
637 sh while.sh
638 ls
639 ls -l
640 chmod u+x while.sh
641 ls -l while.sh
642 pwd
643 /home/18201795718/while.sh
644 ./while.sh
645 cat while.sh
646 cat for.sh
647 mv while.sh while
648 ls -la
649 ./while
650 ls
651 ls -l
652 vi argv.sh
653 chmod +x argv.sh
654 ls
655 ./argv.sh
656 cat argv.sh
657 ps axf|grep argv
658 ls
659 ./argv.sh 1 2 3 4 5 6 7 8 9 10
660 cat argv.sh
661 vi argv.sh
662 ls
663 cat argv.sh
664 ./argv.sh 1 2 3 4 5 6 7 8 9 10
665 vi function.sh
666 chmod +x function.sh
667 ./function.sh
668 vi function.sh
669 bash function.sh
670 vi function.sh
671 bash function.sh
673 echo $PATH
674 mkdir bin
675 mv while bin/
676 while
677 cd bin/
678 ls
679 mv while mywhile
680 mywhile
681 cd
682 ls
683 mywhile
684 ls
685 cd dir5
686 ls
687 mywhile
688 cd
689 ls
690 cat .bashrc
691 echo $hello
692 ls
693 source /etc/profile
694 ls
695 cp bin/mywhile .
696 ls
697 echo $PATH
698 ls
699 cat .bashrc
703 cat for.sh
704 bash for.sh
705 bash -x for.sh
706 vi for.sh
707 bash for.sh
708 cat for.sh
709 which sh
710 which bash
711 ls -l /usr/bin/sh
712 ls
713 cat mywhile
714 cat for.sh
715 echo myvar=1
716 cd
717 ls -l .bashrc
718 cat /etc/profile
719 ls -l
720 ls
721 cat for.sh
723 cat /etc/passwd
724 lls
725 wc -l /etc/passwd
726 who
727 who am i
728 grep 18201795718 /etc/passwd
729 grep -o 18201795718 /etc/passwd
730 grep -on 18201795718 /etc/passwd
731 ps axf|grep grep
732 ls
733 curl baidu.com
734 curl baidu.com | grep 'http://[a-zA-Z0-9\\\\-\\\\.]*'
735 curl baidu.com | grep -o 'http://[a-zA-Z0-9\\\\-\\\\.]*'
736 curl baidu.com | grep -o 'http://[a-zA-Z0-9\\\\-\\\\.]*'
737 ls -l | grep -o 'file.*'
738 ls -l
739 ls -l | grep -o 'file.*|dir'
740 ls -l | grep -oE 'file.*|dir'
741 ls -l | grep -E 'file.*|dir'
742 ls -l | grep -E 'file.*|dir|for'
743 ls
744 cat /etc/passwd
745 grep 1314 /etc/passwd
746 grep 1312 /etc/passwd
747 grep -E '1312|1314|1343' /etc/passwd
748 ls
749 grep -E '1312|1314|1343' /etc/passwd
750 ls -l
751 cat file
752 cat file4
753 grep me file4
754 grep -v me file4
755 ls
756 cat file4
757 cat file5
758 grep --help
759 ls
760 ls -l
761 ls -l | awk '{print $9}'
762 ls -l | awk -F '/' '{print $9}'
763 ls -l | awk -F '/' '{print $1}'
764 ls -l | awk -F '/' '{print $2}'
765 ls -l | awk -F '/' '{print $0}'
766 ls
767 ls -l | awk -F '/' '{print RS}'
768 ls -l | awk '{print RS}'
769 ls
770 ls -l | awk '{print RS}'
771 ls -l | awk '{print FS}'
772 ls -l | awk '{print $1}'
773 ls -l | awk 'BEGIN{print FS}'
774 ls -l | awk 'BEGIN{print RS}'
775 ls
776 echo $PATH
777 echo $PATH | awk 'BEGIN{RS=":"}{print $0}'
778 echo $PATH | awk 'BEGIN{RS="/"}{print $0}'
779 ls
780 ls -l
781 ls -l|awk '{print FS}'
782 ls -l|awk -F ':' '{print FS}'
783 ls
784 ls -l|awk '{print NR}'
785 ls -l
786 ls -l|awk '{print NF}'
787 history
788 ls
789 echo $PATH
790 echo $PATH |awk 'BEGIN{RS=":"}{print $0}'
791 echo $PATH |awk 'BEGIN{RS=":"}{print $0}' | awk -F '/' '{print $1 $2 $3 $4}'
792 vi awk.sh
793 bash awk.sh
794 cat awk.sh
795 bash awk.sh
796 echo $PATH |awk 'BEGIN{RS=":"}{print $0}' | awk -F '/' '{print $1 $2 $3 $4}'
797 echo $PATH |awk 'BEGIN{RS=":"}{print $0}' | awk -F '/' '{print $1" hello " $2 $3 $4}'
798 echo $PATH |awk 'BEGIN{RS=":"}{print $0}' | awk -F '/' '{print $1' hello ' $2 $3 $4}'
799 echo $PATH |awk 'BEGIN{RS=":"}{print $0}' | awk -F '/' '{print $1 "hello " $2 $3 $4}'
800 ls
801 echo $PATH |awk 'BEGIN{RS=":"}{print $0}' | awk -F '/' '{print $1 "hello " $2 $3 $4}'
802 bash awk.sh
803 echo $PATH |awk 'BEGIN{RS=":"}{print $0}'
804 ls
805 bash awk.sh
806 ls
807 vi awk.sh
808 bash awk.sh
809 cat -n awk.sh
810 vi awk.sh
811 bash awk.sh
812 vi awk.sh
813 bash awk.sh
815 cat mywhile
816 sed 's/break/continue/g' mywhile
817 cat mywhile
818 sed -i 's/break/continue/g' mywhile
819 cat mywhile
820 ls
821 sed -i 's/break/continue/g' 's/ls/pwd/g' mywhile
822 sed -i -e 's/break/continue/g' -e 's/ls/pwd/g' mywhile
823 cat mywhile
824 ls -l
825 echo "123|123|345"
826 echo "123|123|345" |awk '{print $1}'
827 echo "123|123|345" |awk -F '|' '{print $1}'
828 echo "123|123|345" |awk 'BEGIN{FS="|"}{print $1}'
829 echo "123|123|345" |awk -F '|' '{print $1}'
834 bc
836 echo "2 <= 4"|bc
846 curl http://baidu.com/s?wd=mp3
847 curl http://baidu.com/s?wd=mp3 2>&1
848 curl http://baidu.com/s?wd=mp3 2>&1|grep -o "结果为[0-9]"*
863 curl http://www.baidu.com/s?wd=mp3 2>&1|grep -o "结果约[0-9,]*"
871 echo "2>1"|bc
872 echo "2>1.5"|bc
873 echo "2>2.5"|bc
874 vi testaverage.sh
875 bash testaverage.sh
876 ps axf|grep vi
877 vi testaverage.sh
878 bash testaverage.sh
879 ps axf|grep vim
880 ls -l|awk '{print $9}'
881 ls -l|awk '{print "文件名:"$9}'
882 ls -l|awk '{print "文件名:"$9}'
883 ls
884 ps axf|grep vim
885 w|grep -E 'pts/41|pts/42'
886 bash testaverage.sh
887 w
888 w|head -1
889 w|head -1|awk '{print $10}'
890 w|head -1|awk '{print $10}'|sed 's/,//g'
891 ps axf|grep vim
892 ps ef|grep vim
893 ps ae|grep vim
894 ps ex|grep vim
895 ls
896 cat testaverage.sh
914 cat testaverage.sh
915 bash testaverage.sh
921 w|head -1
922 w|head -1 |awk '{print $10}'
923 w|head -1 |awk -F 'load' '{print $2}'
924 w|head -1 |awk -F 'load' '{print $2}' |awk '{print $2}'
925 netstat
926 netstat -ntlp
927 netstat -antulp
928 netstat -ntlp