Common Shell script collection

Source: Internet
Author: User

Common Shell script collection

In O & M, especially for linux O & M, we all know the importance of scripts. scripts will make our O & M more efficient. Therefore, learning to write scripts is a task that every linux O & M personnel must learn, here, we will add the scripts commonly used for linux O & M. The most important thing to learn about scripts is a large number of exercises and practices. Based on the following script, we can expand it so that we can improve it very quickly! The opposite is true!

1. Use Shell programming to determine whether a file is a character device file. If it is copied to the/dev directory.


Reference Program:

C code
  1. #! /Bin/sh
  2. FILENAME =
  3. Echo "Inputfilename :"
  4. ReadFILENAME
  5. If [-c "$ FILENAME"]
  6. Then
  7. Cp $ FILENAME/dev
  8. Fi

2. Design a shell program, add a new group named class1, and add 30 users to the group. The username format is stdxx, where xx ranges from 01 to 30.


Reference answer:

C code
  1. #! /Bin/sh
  2. I = 1
  3. Groupaddclass1
  4. While [$ i-le30]
  5. Do
  6. If [$ i-le9]; then
  7. USERNAME = stu0 $ {I}
  8. Else
  9. USERNAME = stu $ {I}
  10. Fi
  11. Useradd $ USERNAME
  12. Mkdir/home/$ USERNAME
  13. Chown-R$ USERNAME/home/$ USERNAME
  14. Chgrp-Rclass1/home/$ USERNAME
  15. I = $ ($ I + 1 ))
  16. Done


3. Write a shell program to automatically delete 50 accounts. The account name is stud1 to stud50.

Reference Program:

C code
  1. #! /Bin/sh
  2. I = 1
  3. While [$ i-le50]
  4. Do
  5. Userdel-rstud $ {I}
  6. I = $ ($ I + 1 ))
  7. Done


4. A system administrator needs to do some repetitive work every day. Prepare a solution according to the following requirements:
(1) Delete All subdirectories and all files under the/abc directory;
(2) From ~ At pm, all data in the first domain of each line in the x1 file under the/xyz directory is read to the bak01.txt file under the/backupdirectory;
(3) archive and compress all directories and files under the/data directory as files at every Monday: backup.tar.gz;
(4) uninstall the CD-ROM of the IDE interface at PM (assuming the device name of the CD-ROM is hdc );
(5) Start the instance before a.m.


Reference answer:
Solution:
(1) Use vi to create and edit a crontab file named prgx;
(2) prgx file content:

C code
  1. 5016 * rm-r/abc /*
  2. 08-18/1 **** cut-f1/xyz/x1>;/backup/bak01.txt
  3. 5017 *** tarzcvfbackup.tar.gz/data
  4. 5517 *** umount/dev/hdc


(3) log on to the Super User and use crontab to execute the content in the prgx file:
Root @ xxx: # crontab prgx; crontab can be automatically started after it is started up before every morning.


5. design a shell program, back up and compress all contents in the/etc directory on the first day of every month, and store them in the/root/bak directory. The file name is in the following format: yymmdd_etc, and yy is a year, mm indicates the month and dd indicates the day. The Shell program fileback is stored in the/usr/bin directory.

Reference answer:
(1) write the shell program fileback:

C code
  1. #! /Bin/sh
  2. DIRNAME = 'ls/root | grepbak'
  3. If [-z "$ DIRNAME"]; then
  4. Mkdir/root/bak
  5. Cd/root/bak
  6. Fi
  7. YY = 'date + % y'
  8. MM = 'date + % m'
  9. DD = 'date + % d'
  10. Backetc00000000yy1_mm1_dd_etc.tar.gz
  11. Tarzcvf $ BACKETC/etc
  12. Echo "filebackfinished !"


(2) Compile the task Timer:

C code
  1. Echo "001 **/bin/sh/usr/bin/fileback">;/root/etcbakcron
  2. Crontab/root/etcbakcron
  3. Or use the crontab-e command to add a scheduled task:
  4. 01 ***/bin/sh/usr/bin/fileback


6. What should a common user do to regularly back up/user/backup to the/tmp directory at 00:00 every Sunday?

Reference answer:

(1) Method 1:

C code
  1. You should use the crontab-e command to create the crontab file. The format is as follows:
  2. 00 ** suncp-r/user/backup/tmp


(2) Method 2:
Create a file in your directory. The file content is as follows:

C code
  1. 0 ** suncp-r/user/backup/tmp

Then execute crontab file to make it take effect.


7. Design a Shell program and create 50 directories under the/userdata directory, that is, user1 ~ User50, and set the permissions for each directory. the permissions of other users are: read; the permissions of the file owner are: read, write, and execute; and the permissions of the group where the file owner is located are: read and execute.

Reference answer: create a program Pro16 as follows:

C code
  1. #! /Bin/sh
  2. I = 1
  3. While [i-le50]
  4. Do
  5. If [-d/userdata]; then
  6. Mkdir-p/userdata/user $ I
  7. Chmod754/userdata/user $ I
  8. Echo "user $ I"
  9. Let "I = I + 1" (or I = $ ($ I + 1 ))
  10. Else
  11. Mkdir/userdata
  12. Mkdir-p/userdata/user $ I
  13. Chmod754/userdata/user $ I
  14. Echo "user $ I"
  15. Let "I = I + 1" (or I = $ ($ I + 1 ))
  16. Fi
  17. Done

8. mysql backs up an instance, automatically backs up mysql, and deletes the backup files 30 days ago.

C code
  1. #! /Bin/sh
  2. # Autobackupmysql
  3. # Wugk2012-07-14
  4. # PATHDEFINE
  5. BAKDIR =/data/backup/mysql/'date + % Y-% m-% d'
  6. MYSQLDB = www
  7. MYSQLPW = backup
  8. MYSQLUSR = backup
  9. If [$ UID-ne0]; then
  10. EchoThisscriptmustuseadministratororrootuser, pleaseexit!
  11. Sleep2
  12. Exit0
  13. Fi
  14. If [! -D $ BAKDIR]; then
  15. Mkdir-p $ BAKDIR
  16. Else
  17. EchoThisis $ BAKDIRexists, pleaseexit ....
  18. Sleep2
  19. Exit
  20. Fi
  21. ### Mysqldumpbackupmysql
  22. /Usr/bin/mysqldump-u $ MYSQLUSR-p $ MYSQLPW-d $ MYSQLDB>/data/backup/mysql/'date + % Y-% m-% D'/www_db. SQL
  23. Cd1_bakdir1_tar-czfwww_mysql_db.tar.gz *. SQL
  24. Cd $ BAKDIR; find.-name "*. SQL" | xargsrm-rf [$? -Eq0] & echo "This 'date + % Y-% m-% d' RESINBACKUPisSUCCESS"
  25. Cd/data/backup/mysql/; find.-mtime + 30 | xargsrm-rf

9. automatically install the Nginx script. Use the case method. You can also change the script you want based on your actual needs.

C code
  1. #! /Bin/sh
  2. ### Nginxinstallshell
  3. ### Wugk2012-07-14
  4. ### PATHDEFINE
  5. SOFT_PATH =/data/soft/
  6. Nginx_file1_nginx-1.2.0.tar.gz
  7. DOWN_PATH = http://nginx.org/download/
  8. If [$ UID-ne0]; then
  9. EchoThisscriptmustuseadministratororrootuser, pleaseexit!
  10. Sleep2
  11. Exit0
  12. Fi
  13. If [! -D $ SOFT_PATH]; then
  14. Mkdir-p $ SOFT_PATH
  15. Fi
  16. Download ()
  17. {
  18. Cd $ SOFT_PATH; wget $ DOWN_PATH/$ NGINX_FILE
  19. }
  20. Install ()
  21. {
  22. Yuminstallpcre-devel-y
  23. Cd $ SOFT_PATH; tarxzf $ NGINX_FILE; cdnginx-1.2.0/&./configure-prefix =/usr/local/nginx/-with-http_stub_status_module-with-http_ssl_module
  24. [$? -Eq0] & make & makeinstall
  25. }
  26. Start ()
  27. {
  28. Lsof-I: 80 [$? -Ne0] & amp;/usr/local/nginx/sbin/nginx
  29. }
  30. Stop ()
  31. {
  32. Ps-ef | grepnginx | grep-vgrep | awk '{print $2}' | xargskill-9
  33. }
  34. Exit ()
  35. {
  36. Echo $ ?; Exit
  37. }
  38. ### Casemenu #####
  39. Case $ 1in
  40. Download)
  41. Download
  42. ;;
  43. Install)
  44. Install
  45. ;;
  46. Start)
  47. Start
  48. ;;
  49. Stop)
  50. Stop
  51. ;;
  52. *)
  53. Echo "USAGE: $0 {downloadorinstallorstartorstop }"
  54. Exit
  55. Esac

10. decompress the tar script in batches, decompress the zip file in batches, and create the current directory.

C code
  1. #! /Bin/sh
  2. PATH1 =/tmp/images
  3. PATH2 =/usr/www/images
  4. Foriin 'ls ${PATH1 }/*'
  5. Do
  6. Tarxvf $ I-C $ PATH2
  7. Done

This script targets all tar files in one directory, but in actual situations, it may be in a lower-level or deeper directory. We can use find to find

C code
  1. #! /Bin/sh
  2. PATH1 =/tmp/images
  3. PATH2 =/usr/www/images
  4. Foriin 'Find $ PATH1-name "*. tar "'
  5. Do
  6. Tarxvf $ I-C $ PATH2
  7. Done

For example, for batch files such as 123189.zip 132342.zip, unzipdirectly uncompress the image without self-recorded content. The idea is to decompress 123189.zip. The current directory is an image. You cannot create and decompress the 123189 directory. You can use shell scripts.

C code
  1. #! /Bin/sh
  2. PATH1 =/tmp/images
  3. PATH2 =/usr/www/images
  4. Cd $ PATH1
  5. Foriin 'Find.-name "*. zip" | awk-F. {print $2 }'
  6. Do
  7. Mkdir-pPATH2 $ I
  8. Unzip-o.polici.zip-dPATH2 $ I
  9. Done

Original article reprinted, please note: Common Shell scripts in Linux | focus on the Unix/Linux Field

Upload files in batches Using ssh

Most of the files to be uploaded use ftp, but it is not good to use ftp, that is, to correspond to local and remote directories. In this way, it is very troublesome to switch between multiple directories, if you do not pay attention to it, it is very likely that an error will be reported. So I tried to use scp to upload files or directories in batches.

I. Do not enter a password for scp uploads

If you want to use scp to upload files, you need to enter a password when removing scp from the first step. Otherwise, you cannot upload the files in batches. For details, see:Ssh password is not required

Ii. ssh batch upload scripts

1. Put the list of files to be uploaded into a test file.

C code
  1. Root @ ubuntu:/home/zhangy # cattest
  2. /Home/zhangy/test/aaa
  3. /Home/zhangy/test/nginx. conf
  4. /Home/zhangy/test. SQL
  5. /Home/zhangy/test/pa.txt
  6. /Home/zhangy/test/password

The file to be uploaded.

2. Batch upload scripts

Vim file_upload.sh

C code
  1. #! /Bin/sh
  2. DATE = 'date + % Y _ % m _ % d _ % H'
  3. If [$1]
  4. Then
  5. Forfilein $ (sed '/^ $/d' $1) // Remove empty rows
  6. Do
  7. If [-f $ file] // common file
  8. Then
  9. Res = 'scp $ file $2: $ file' // upload a file
  10. If [-z $ res] // upload successful
  11. Then
  12. Echo $ file >>$ {DATE} _ upload. log // logs of successful upload
  13. Fi
  14. Elif [-d $ file] // directory
  15. Then
  16. Res = 'scp-r$ file $2: $ file'
  17. If [-z $ res]
  18. Then
  19. Echo $ file >>$ {DATE} _ upload. log
  20. Fi
  21. Fi
  22. Done
  23. Else
  24. Echo "nofile" >> {DATE} _ error. log
  25. Fi

After the upload is successful, an empty row is returned. If the upload fails, nothing is returned.

3. Upload format

C code
  1. ./File_upload.shtest192.168.1.13

Test is the place where the file 192.168.1.13 is to be uploaded.

0

Reprinted please note
Author: sea bottom e
Address: http://blog.51yip.com/linux/1356.html

1. convert file case:

A script to convert the specified filenames to lower case.

C code
  1. #! /Bin/sh
  2. # Lowerit
  3. # Convertallfilenamesinthecurrentdirectorytolowercase
  4. # Onlyoperatesonplainfiles -- doesnotchangethenameofdirectories
  5. # Willaskforverificationbeforeoverwritinganexistingfile
  6. Forxin 'LS'
  7. Do
  8. If [! -F $ x]; then
  9. Continue
  10. Fi
  11. Lc = 'echo $ x | tr' [A-Z] ''[a-z]''
  12. If [$ lc! = $ X]; then
  13. Mv-I $ x $ lc
  14. Fi
  15. Done

Or

C code
  1. Iftest $ # = 0
  2. Then
  3. Echo "Usage $0: <files>" 1> & 2
  4. Exit1
  5. Fi
  6. Forfilenamein "$ @"
  7. Do
  8. New_filename = 'echo "$ filename" | trA-Za-Z'
  9. Test "$ filename" = "$ new_filename" & continue
  10. Iftest-r "$ new_filename"
  11. Then
  12. Echo "$0: $ new_filenameexists" 1> & 2
  13. Eliftest-e "$ filename"
  14. Then
  15. Mv "$ filename" "$ new_filename"
  16. Else
  17. Echo "$0: $ filenamenotfound" 1> & 2
  18. Fi
  19. Done

2. Watch a Website

A script to repeated download a webpage until it matches a regex then bought y an e-mail address.

For example to get e-mail when Kesha tickets (not for yourself of course) go on sale you might run:

C code
  1. % Watch_website.shhttp: // ticketek.com. au/'ke [sS $] + ha' andrewt @ cse.unsw.edu. au
  2. Repeat_seconds = 300 # checkevery5minutes
  3. Iftest $ # = 3
  4. Then
  5. Url = $1
  6. Regexp = $2
  7. Email_address = $3
  8. Else
  9. Echo "Usage: $0 <url> <regex>" 1> & 2
  10. Exit1
  11. Fi
  12. Whiletrue
  13. Do
  14. Ifwget-O -- q "$ url" | egrep "$ regexp">/dev/null
  15. Then
  16. Echo "Generatedby $0" | mail-s "$ urlnowmatches $ regexp" $ email_address
  17. Exit0
  18. Fi
  19. Sleep $ repeat_seconds
  20. Done

3. Convert GIF to PNGconvert GIF files to PNG

This scripts converts GIF files to PNG files via the intermediate PPM format.

C code
  1. If [$ #-eq0]
  2. Then
  3. Echo "Usage: $ 0files..." 1> & 2
  4. Exit1
  5. Fi
  6. If! Typegiftopnm2>/dev/null
  7. Then
  8. Echo "$0: conversiontoolgiftopnmnotfound" 1> & 2
  9. Exit1
  10. Fi
  11. # Missing "in..." defaultstoin "$ @"
  12. Forf
  13. Do
  14. Case "$ f" in
  15. *. Gif)
  16. # OK, donothing
  17. ;;
  18. *)
  19. Echo "gif2png: skipping $ f, notGIF"
  20. Continue
  21. ;;
  22. Esac
  23. Dir = 'dirname "$ f "'
  24. Base = 'basename "$ f". gif'
  25. Result = "$ dir/export base.png"
  26. Giftopnm "$ f" | pnmtopng> $ result & echo "wrote $ result"
  27. Done

4. Count Counting

A utility script to print the sub-range of integers specified by its arguments.

Useful to use on the command line or from other scripts

C code
  1. Iftest $ # = 1
  2. Then
  3. Start = 1
  4. Finish = $1
  5. Eliftest $ # = 2
  6. Then
  7. Start = $1
  8. Finish = $2
  9. Else
  10. Echo "Usage: $0 <start> <finish>" 1> & 2
  11. Exit1
  12. Fi
  13. Forargumentin "$ @"
  14. Do
  15. Ifecho "$ argument" | egrep-v '^ -? [0-9] + $ '>/dev/null
  16. Then
  17. Echo "$0: argument '$ argument' isnotaninteger" 1> & 2
  18. Exit1
  19. Fi
  20. Done
  21. Number = $ start
  22. Whiletest $ number-le $ finish
  23. Do
  24. Echo $ number
  25. Number = 'expr $ number + 1' # ornumber = $ ($ number + 1 ))
  26. Done

5. Word Frequency

Count the number of time each different word occurs in the files given as arguments.

C code
  1. Sed's // \ n/G' "$ @" | # converttoonewordperline
  2. TrA-Za-z | # mapuppercasetolowercase
  3. Sed "s/[^ a-Z'] // g" | # removeallcharactersexcepta-zand'
  4. Egrep-v '^ $' | # removeemptylines
  5. Sort | # placewordsinalphabeticalorder
  6. Uniq-c | # useuniqtocounthowmanytimeseachwordoccurs
  7. Sort-n # orderwordsinfrequencyofoccurrance
For example

C code
  1. % Cd/home/cs2041/public_html/lec/shell/examples
  2. %./Word_frequency.shdracula.txt | tail
  3. 2124it
  4. 2440 that
  5. 2486in
  6. 2549he
  7. 2911a
  8. 3600of
  9. 4448to
  10. 4740i
  11. 5833and
  12. 7843the

6. Finding

Search $ PATH for the specified programs

C code
  1. Iftest $ # = 0
  2. Then
  3. Echo "Usage $0: <program>" 1> & 2
  4. Exit1
  5. Fi
  6. Forprogramin "$ @"
  7. Do
  8. Program_found =''
  9. Fordirectoryin 'echo "$ PATH" | tr ':''''
  10. Do
  11. F = "$ directory/$ program"
  12. Iftest-x "$ f"
  13. Then
  14. Ls-ld "$ f"
  15. Program_found = 1
  16. Fi
  17. Done
  18. Iftest-z $ program_found
  19. Then
  20. Echo "$ programnotfound"
  21. Fi
  22. Done

Alternative implementation using while, and a cute use of grep and |

C code
  1. Iftest $ # = 0
  2. Then
  3. Echo "Usage $0: <program>" 1> & 2
  4. Exit1
  5. Fi
  6. Forprogramin "$ @"
  7. Do
  8. Echo "$ PATH" |
  9. Tr': ''\ n' |
  10. Whilereaddirectory
  11. Do
  12. F = "$ directory/$ program"
  13. Iftest-x "$ f"
  14. Then
  15. Ls-ld "$ f"
  16. Fi
  17. Done |
  18. Egrep '.' | echo "$ programnotfound"
  19. Done

And another implementation using while, and a cute use of grep and |

C code
  1. Iftest $ # = 0
  2. Then
  3. Echo "Usage $0: <program>" 1> & 2
  4. Exit1
  5. Fi
  6. Forprogramin "$ @"
  7. Do
  8. N_path_components = 'echo $ PATH | tr-d-c: | wc-C'
  9. Index = 1
  10. Whiletest $ index-le $ n_path_components
  11. Do
  12. Directory = 'echo "$ PATH" | cut-d:-f $ Index'
  13. F = "$ directory/$ program"
  14. Iftest-x "$ f"
  15. Then
  16. Ls-ld "$ f"
  17. Program_found = 1
  18. Fi
  19. Index = 'expr $ index + 1'
  20. Done
  21. Test-n $ program_found | echo "$ programnotfound"
  22. Done

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.