Part 2: Linux Shell advanced programming skills-Chapter 2 shell tools

Source: Internet
Author: User
Tags tmp folder
Document directory
  • Notes
  • Diagram
Notes
# Shell tool # log file # edit # vi datelog. Sh # View File Content [root @ localhost 0418] # Cat datelog. Sh #! /Bin/bash # datelog. sh # current date current_date = 'date "+ % Y % m % d" '# Today's log file name todaylog = "log/$ {current_date }. log} # If the log file does not exist, create an if [! -F $ todaylog] Then touch $ todaylogfi # output the log to the log file log_time_format = 'date "+ % Y-% m-% d % t" 'echo "$ {log_time_format} start "> $ todaylog # command blockssleep 4 # output the log to the log file log_time_format = 'date" + % Y-% m-% d % t "'echo" $ {log_time_format} command end "> $ todaylog # Run [root @ localhost 0418] #. /datelog. sh # view the current date [root @ localhost 0418] # datethu Apr 18 11:35:52 CST 2013you have new mail in/var/spool/mail/root # view the date help manual [root @ Lo Calhost 0418] # Man date # edit VI processlog. sh # Change permission [root @ localhost 0418] # chmod 755 processlog. sh # Start HTTPd service [root @ localhost 0418] # service httpd start starting httpd: Perl: Warning: Setting locale failed. perl: Warning: Please check that your locale settings: Language = (unset), lc_all = (unset), lang = "en_us.en" are supported and installed on your system. perl: Warning: falling back to the standard Loc ALE ("C "). [OK] # view HTTPd process details and remove grep [root @ localhost 0418] # ps-Aux | grep "/usr/sbin/httpd" | grep-V "grep" Root 19202 1.9 3.3 19656 8624? S/usr/sbin/httpd # view HTTPd process details and remove grep and print process numbers [root @ localhost 0418] # ps-Aux | grep "/usr/sbin/ httpd "| grep-V" grep "| awk '{print $2}' 19202 # Run [root @ localhost 0418] #. /processlog. sh # view the [root @ localhost 0418] # Cat/ttftpboot TMP folder and find the 19304.txt file [root @ localhost 0418] # Cat/tmp/.x0-lock.x11-unix.fam_socket.font-unix.gdm_socket.iroha_unix19304.txt osl_pipe_0_singleofficeipc_e31798b4554fdedd4 Contents of the statement [root @ localhost 0418] # Cat/tmp/19304.txt 19202 # manually delete [root @ localhost 0418] # rm-F/tmp/19304.txt # Run [root @ localhost 0418] #. /processlog. sh Rm-F/tmp/19421.txt# check again and find no content [root @ localhost 0418] # Cat/tmp/19421.txt Cat: /tmp/19421.txt: no such file or directory # signal # view all signals [root @ localhost 0418] # Kill-L 1) sighup 2) SIGINT 3) sigquit 4) sigill 5) sigtrap 6) SIGABRT 7) sigbus 8) sigfpe 9) sigkill 10) SIGUSR1 11) SIGSEGV 12) sigusr213) sigpipe 14) sigalrm 15) sigterm 17) sigchld18) sigcont 19) sigstop 20) sigtstp 21) sigttin22) sigttou 23) sigurg 24) sigxcpu 25) sigxfsz26) sigvtalrm 27) sigprof 28) sigwinch 29) sigio30) sigpwr 31) sigsys 33) sigrtmin 34) sigrtmin + 135) sigrtmin + 2 36) sigrtmin + 3 37) sigrtmin + 4 38) sigrtmin + 539) sigrtmin + 6 40) sigrtmin + 7 41) sigrtmin + 8 42) sigrtmin + 943) sigrtmin + 10 44) sigrtmin + 11 45) sigrtmin + 12 46) sigrtmin + 1347) sigrtmin + 14 48) sigrtmin + 15 49) SIGRTMAX-14 SIGRTMAX-1351 50) SIGRTMAX-12 52) SIGRTMAX-11 53) SIGRTMAX-10 54) SIGRTMAX-955 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-559) SIGRTMAX-4 60) SIGRTMAX-3 61) SIGRTMAX-2 62) SIGRTMAX-163) sigrtmax # view httpd Process details [root @ localhost 0418] # ps-Ef | grep "httpd" root 19202 1 0? 00:00:00/usr/sbin/httpdapache 19205 19202 0? 00:00:00 [httpd] Apache 19206 19202 0? 00:00:00 [httpd] Apache 19207 19202 0? 00:00:00 [httpd] Apache 19208 19202 0? 00:00:00 [httpd] Apache 19209 19202 0? 00:00:00 [httpd] Apache 19210 19202 0? 00:00:00 [httpd] Apache 19211 19202 0? 00:00:00 [httpd] Apache 19212 19202 0? 00:00:00 [httpd] root 19533 2658 0 00:00:00 pts/0 grep httpd # view HTTPd process details and remove grep [root @ localhost 0418] # ps-Ef | grep "httpd" | grep-V "grep" root 19202 1 0? 00:00:00/usr/sbin/httpdapache 19205 19202 0? 00:00:00 [httpd] Apache 19206 19202 0? 00:00:00 [httpd] Apache 19207 19202 0? 00:00:00 [httpd] Apache 19208 19202 0? 00:00:00 [httpd] Apache 19209 19202 0? 00:00:00 [httpd] Apache 19210 19202 0? 00:00:00 [httpd] Apache 19211 19202 0? 00:00:00 [httpd] Apache 19212 19202 0? 00:00:00 [httpd] # Kill process 19212 [root @ localhost 0418] # Kill-s sigkill 19212 [root @ localhost 0418] # ps-Ef | grep "httpd" | grep-V" grep "root 19202 1 0? 00:00:00/usr/sbin/httpdapache 19205 19202 0? 00:00:00 [httpd] Apache 19206 19202 0? 00:00:00 [httpd] Apache 19207 19202 0? 00:00:00 [httpd] Apache 19208 19202 0? 00:00:00 [httpd] Apache 19209 19202 0? 00:00:00 [httpd] Apache 19210 19202 0? 00:00:00 [httpd] Apache 19211 19202 0? 00:00:00 [httpd] # function same as [root @ localhost 0418] # Kill-9 19211 # re-view the HTTPd process details and remove grep. No such process exists, indicates that the [root @ localhost 0418] is terminated # ps-Ef | grep "httpd" | grep-V "grep" root 19202 1 0? 00:00:00/usr/sbin/httpdapache 19205 19202 0? 00:00:00 [httpd] Apache 19206 19202 0? 00:00:00 [httpd] Apache 19207 19202 0? 00:00:00 [httpd] Apache 19208 19202 0? 00:00:00 [httpd] Apache 19209 19202 0? 00:00:00 [httpd] Apache 19210 19202 0? 00:00:00 [httpd] # suspending a process [root @ localhost 0418] # Kill-s sighup 19210 # viewing HTTPd process details and removing grep [root @ localhost 0418] # ps-Ef | grep "httpd" | grep-V "grep" root 19202 1 0 12:09? 00:00:00/usr/sbin/httpdapache 19205 19202 0? 00:00:00 [httpd] Apache 19206 19202 0? 00:00:00 [httpd] Apache 19207 19202 0? 00:00:00 [httpd] Apache 19208 19202 0? 00:00:00 [httpd] Apache 19209 19202 0? 00:00:00 [httpd] # view HTTPd process details and remove grep [root @ localhost 0418] # ps-Ef | grep "httpd" | grep-V "grep" root 19202 1 0 12:09? 00:00:00/usr/sbin/httpdapache 19205 19202 0? 00:00:00 [httpd] Apache 19206 19202 0? 00:00:00 [httpd] Apache 19207 19202 0? 00:00:00 [httpd] Apache 19208 19202 0? 00:00:00 [httpd] Apache 19209 19202 0? 00:00:00 [httpd] # Kill sub-processes or all sub-processes under the parent process [root @ localhost 0418] # Kill-1 19209 # view HTTPd process details and remove grep [root @ localhost 0418] # ps-Ef | grep "httpd" | grep-V "grep" root 19202 1 0? 00:00:00/usr/sbin/httpdapache 19205 19202 0? 00:00:00 [httpd] Apache 19206 19202 0? 00:00:00 [httpd] Apache 19207 19202 0? 00:00:00 [httpd] Apache 19208 19202 0? 00:00:00 [httpd] Apache 19616 19202 0? 00:00:00 [httpd] # view HTTPd process details and remove grep [root @ localhost 0418] # ps-Ef | grep "httpd" | grep-V "grep" root 19202 1 0? 00:00:00/usr/sbin/httpdapache 19205 19202 0? 00:00:00 [httpd] Apache 19206 19202 0? 00:00:00 [httpd] Apache 19207 19202 0? 00:00:00 [httpd] Apache 19208 19202 0? 00:00:00 [httpd] Apache 19616 19202 0? 00:00:00 [httpd] # process with the ending process number 19202 [root @ localhost 0418] # Kill-1 19202 # view HTTPd process details and remove grep [root @ localhost 0418] # ps- EF | grep "httpd" | grep-V "grep" root 19202 1 0? 00:00:00/usr/sbin/httpdexit crtl + D Exit the current shell # trap capture signal # create a folder 0419 mkdir 0419 # Enter the directory CD 0419 # copy all files to the current directory 0418 [root @ localhost 0419] # cp .. /0418 /*. # edit [root @ localhost 0419] # vi trap1.sh # View content [root @ localhost 0419] # Cat trap1.sh #! /Bin/bash # trap1.sh # note that this is a single quotation mark, not a reverse quotation mark trap 'exitprocess' 2 loop = 0 Function exitprocess () {echo "you just hit <CRTL-C>, at number $ loop "Echo" I will now exit "Exit 1} while: do Loop = $ [$ loop + 1] echo $ loopdone # Change permission [root @ localhost 0419] # chmod 755 trap1.sh # Run [root @ localhost 0419] #. /trap1.sh # view the process on another terminal [root @ localhost 0419] # ps-efroot 4221 2113 0? 00:00:00/usr/sbin/sshdroot 4241 4221 0 00:00:00 pts/1 00:00:00-bashroot 4834 4180 6 pts/0/bin/bash. /trap1.sh # Stop the process kill-2 4834 # Run the terminal to print the information you just hit <CRTL-C>, at number 38383i will now exit # re-edit [root @ localhost 0419] # vi trap1.sh # view the content and add sleep call [root @ localhost 0419] # Cat trap1.sh #! /Bin/bash # trap1.shtrap 'exitprocess' 2 loop = 0 Function exitprocess () {echo "you just hit <CRTL-C>, at number $ loop "Echo" I will now exit "Exit 1} while: do Loop = $ [$ loop + 1] echo $ loop # Add code sleep 1 done # Run [root @ localhost 0419] #. /trap1.shroot 5245 4180 0 00:00:00 pts/0/bin/bash. /trap1.sh # end the process kill-4 5245 # current terminal, display Information illegal instruction # Do not process kill-3 5305 # process kill-2 5305 # copy the file [root @ localhost 0419] # cp trap1.sh trap2.sh # View content [root @ localhost 0419] # Cat trap2.sh #! /Bin/bash # trap2.shloop = 0 trap 'exitprocess' 2hold1 =/tmp/ho1d1. $ hold2 =/tmp/hold2. $ function exitprocess () {echo-e "\ nrecived interrupt... "echo-n" Do you really wish to exit? (Y? N) "read ans case $ ans in Y | Y) rm_tmp_file; n | N); *) exitprocess; esac} function rm_tmp_file () {echo "<CRTL-C> detected .. now cleaning up... wait "RM/tmp /*. $2>/dev/null Exit 1} while: do Loop = $ [$ loop + 1] echo $ loop DF >>1 hold1 PS-Xa >>$ hold2 sleep 1 done # Run [root @ localhost 0419] #. /trap2.sh restart Ived interrupt... Do you really wish to exit? (Y? N) n565758596061626364656667686970717273747576recived interrupt... Do you really wish to exit? (Y? N) Y <CRTL-C> detected .. now cleaning up... wait # view the content in the tmp directory LS-Al/tmp/-RW-r -- 1 Root 3816 Apr 19 ho1d1.5503-RW-r -- 1 Root 3816 apr 19 ho1d2.5503 # view the process PS-Ef # End Process kill-2 5503 # eval # Run [root @ localhost 0419] # myfile = "cat myfile "; 'eval $ myfile'-Bash: Total: command not found # edit VI myfile # View content [root @ localhost 0419] # Cat myfile LS-Al # Run [root @ localhost 0419] # myfile = "cat myfile "; 'eval $ myfile' total 32drwxr-xr-x 2 root Root 4096 Apr 19 :54 54. drwxr-XR-x 15 Root 4096 Apr 19 .. -rwxr-XR-x 1 Root 431 Apr 19 awkif. sh-rwxr-XR-x 1 Root 435 Apr 19 datelog. sh-RW-r -- 1 Root 7 Apr 19 myfile-rwxr-XR-x 1 Root 391 Apr 19 processlog. sh-rwxr-XR-x 1 Root 210 Apr 19 trap1.sh-rwxr-XR-x 1 Root 484 Apr 19 trap2.sh # re-edit [root @ localhost 0419] # vi myfile # View content [root @ localhost 0419] # Cat myfile ls # re-execute [root @ localhost 0419] # myfile = "cat myfile "; 'eval $ myfile' awkif. sh datelog. sh myfile processlog. sh trap1.sh trap2.sh # The variable [root @ localhost 0419] # eval 'cat myfile' awkif. sh datelog. sh myfile processlog. sh trap1.sh trap2.sh # logger # view messages [root @ localhost 0419] # Cat/var/log/messages # add content to the log file [root @ localhost 0419] # logger-I" chinaitlab Shenzhen "Apr 19 17:01:09 localhost root [6179]: chinaitlab Shenzhen [root @ localhost 0419] # logger-p 19-I "chinaitlab Shenzhen" # view [root @ localhost 0419] # Cat/var/log/messagesapr 19 17:01:09 localhost Root [6179]: chinaitlab shenzhenapr 19 17:01:56 localhost root [6199]: chinaitlab Shenzhen # add content to the log file [root @ localhost 0419] # logger-P 1-I "chinaitlab Shenzhen" # view [root @ localhost 0419] # Cat/var/log /messagesapr 19 17:01:09 localhost root [0, 6179]: chinaitlab shenzhenapr 19 17:01:56 localhost root [6199]: chinaitlab shenzhenapr 19 17:02:25 localhost root [6201]: chinaitlab Shenzhen

Diagram

 

 

 

 

 

 

 

 

 

@ Wentasy blog is for your reference only. Welcome to visit. I hope to criticize and correct any mistakes. If you need to repost the original blog post, please indicate the source. Thank you for the [csdn blog]
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.