10 excellent Linuxshell script interview questions

Source: Internet
Author: User
Very good ten Linuxshell script interview questions 1. write a shell script to transfer files larger than 10 kb in the current directory to the/tmp Directory #/bin/sh # Programm: # Usingformovecurrentlydirectoryto/tmpforFileNamein 'ls-l |... very good ten Linux shell script interview questions 1. write a shell script to transfer files larger than 10 kb in the current directory to the/tmp Directory #/bin/sh # Programm: # Using for move currently directory to/tmp for FileName in 'ls-l | awk '$5> 10240 {print $9} ''do mv $ FileName/tmp done ls-al /tmp echo "Done! 2. write a shell script to get the network address of the local machine. For example, if the local IP address is 192.168.100.2/255.255.255.0, then its network address is 192.168.100.1/255.255.255.0 Method 1 :#! /Bin/bash # This script print ip and network file = "/etc/sysconfig/network-scripts/ifcfg-eth0" if [-f $ file]; then IP = 'grep "IPADDR" $ file | awk-F "=" '{print $2} ''MASK = 'grep" NETMASK "$ file | awk-F" = "'{print $2} ''echo" $ IP/$ MASK "exit 1 fi Method 2: #! /Bin/bash # This programm will printf ip/network # IP = 'ifconfig eth0 | grep 'inet '| sed's/^. * addr: // g' | sed's/Bcast. * $ // g'' NETMASK = 'ifconfig eth0 | grep 'inet '| sed's/^. * Mask: // g''echo "$ IP/$ NETMASK" exit 3. use Shell programming to determine whether a file is a character device file, if you copy it to the/dev directory. Reference Program :#! /Bin/sh FILENAME = echo "Input file name:" read FILENAME if [-c "$ FILENAME"] then cp $ FILENAME/dev fi 4. add comments for the following shell programs and describe the functions and call methods of the program :#! /Bin/sh #/etc/rc. d/rc. httpd # Start/stop/restart the Apache web server. # To make Apache start automatically at boot, make this # file executable: chmod 755/etc/rc. d/rc. httpd # case "$1" in 'start')/usr/sbin/apachectl start; 'stop')/usr/sbin/apachectl stop; 'restart ') /usr/sbin/apachectl restart; *) echo "usage $0 start | stop | restart"; esac reference answer: (1) program comment #! /Bin/sh defines the practical shell #/etc/rc. d/rc. httpd annotation line. all rows starting with the star number are annotation lines. # Start/stop/restart the Apache web server. # To make Apache start automatically at boot, make this # file executable: chmod 755/etc/rc. d/rc. httpd # case "$1" in # start of the case structure and determine the operation to be executed based on the "location parameter. This program carries a "location parameter", that is, $1 'start') # if the location parameter is start/usr/sbin/apachectl start; # start the httpd process 'stop ') # If the location parameter is stop/usr/sbin/apachectl stop; # stop the httpd process 'restart') # if the location parameter is stop/usr/sbin/apachectl restart ;; # restart the httpd process *) # if the location parameter is not start, stop, or restart, echo "usage $0 start | stop | restart"; # display command prompt information: program call method esac # case structure end (2) the function of the program is to start, stop or restart the httpd process (3) There are three ways to call the program: start, stop and restart. 5. 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 :#! /Bin/sh I = 1 groupadd class1 while [$ I-le 30] do if [$ I-le 9]; then USERNAME = stu0 $ {I} else USERNAME = stu $ {I} fi useradd $ USERNAME mkdir/home/$ USERNAME chown-R $ USERNAME/home/$ USERNAME chgrp-R class1 /home/$ USERNAME I =$ ($ I + 1 )) done 6. write a shell program to automatically delete 50 accounts. The account name is stud1 to stud50. Reference Program :#! /Bin/sh I = 1 while [$ I-le 50] do userdel-r stud $ {I} I = $ ($ I + 1) done 7. A system administrator needs to do some repetitive work every day. prepare a solution according to the following requirements: (1) at 4 pm: 50 delete all subdirectories and all files in 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 at pm every Monday as files: backup.tar.gz; (4) unmount the CD-ROM of the IDE interface at PM (assuming: the device name for the CD-ROM is hdc); (5) it starts up before am. Reference answer: Solution: (1) use vi to create and edit a crontab file named prgx; prgx file content: 50 16 * rm-r/abc/* (2), 0 8-18/1 *** cut-f1/xyz/x1>;/backup/bak01.txt (3), 50 17 *** tar zcvf backup.tar.gz/data (4), 55 17 * umount/dev/hdc (5), logged on by a super user, and used 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. ---------------------------------------------- 8. 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 :#! /Bin/sh DIRNAME = 'ls/root | grep bak 'if [-z "$ DIRNAME"]; then mkdir/root/bak cd/root/bak fi YY = 'date + % Y' MM = 'date + % M' DD = 'date + % d' backetc1_1_yy1_mm1_dd_etc.tar.gz tar zcvf $ BACKETC/etc echo "fileback finished! "(2) compile the task timer: echo" 0 0 1 **/bin/sh/usr/bin/fileback ">; /root/etcbakcron crontab/root/etcbakcron or use the crontab-e command to add a scheduled task: 0 1 ***/bin/sh/usr/bin/fileback 9. A common user wants to regularly back up/user/backup to the/tmp directory at 00:00 every Sunday. what should he do? (1) Method 1: Use the crontab-e command to create a crontab file. The format is as follows: 0 0 ** sun cp-r/user/backup/tmp (2) Method 2: create a file in your Directory. the file content is as follows: 0 ** sun cp-r/user/backup/tmp and then execute crontab file to make it take effect. 10. 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 :#! /Bin/sh I = 1 while [I-le 50] do if [-d/userdata]; then mkdir-p-m 754/userdata/user $ I with-m 754, you don't need to write the following sentence-p is a recursive Directory # chmod 754/userdata/user $ I echo "user $ I" let "I = I + 1" (or I = $ ($ I + 1 )) else mkdir/userdata mkdir-p-m/userdata/user $ I # chmod 754/userdata/user $ I echo "user $ I" let "I = I + 1" (or I = $ ($ I + 1 )) fi done
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.