10 Linux Script interview questions, see how many you can answer?

Source: Internet
Author: User

1. Write a script to determine whether the shell of all users on the current system is a login shell (that is, the user's shell is not/sbin/nologin);
The number of these two categories of users, through string comparison to achieve;

 declare -a shell        declare sum_login=0         declare sum_nologin=0         shell= ($ (CAT&NBSP;/ETC/PASSWD&NBSP;|&NBSP;CUT&NBSP;-D:&NBSP;-F7))          for ((i=0;i<${#shell [*]};i++));d o            if [   "${shell[i]}"  =  "/sbin/nologin"  ];then                  sum_login=$[$sum _login+1]               else                  sum_nologin=$[$sum _nologin+1]            fi         Done        echo  "nologin users is  $sum _nologin."         echo  "login users is  $sum _login."

2, write a script
(1) Gets the host name of the current host, is stored in the hostname variable;
(2) determines whether the value of this variable is localhost, and if so, modifies the current hostname to www.magedu.com;
(3) Otherwise, The current host name is displayed;

#!/bin/bash          hostname=$ (CAT&NBSP;/ETC/SYSCONFIG/NETWORK&NBSP;|&NBSP;GREP&NBSP;HOSTNAME&NBSP;|&NBSP;CUT&NBSP;-D=&NBSP;-F2)          if [  "$hostname"  =  "localhost"   ];then           sed -i  "s/localhost/ Www.magedu.com/g "  /etc/sysconfig/network         else            echo $ (hostname)           fi 

3, write a script, complete the following functions
(1) Pass a disk device file path to the script to determine whether the device exists;
(2) displays all the partition information on this device if it exists;

#!/bin/bash           fdisk -l $1 &> /dev/null;           if [ $? -eq 0 ];then              fdisk -l $1            else              echo  "Fisk  is not found! "           fi 

4, write a script to complete the following functions
The script can accept a parameter;
(1) If the argument 1 is quit, the exit script is displayed and a normal exit is performed;
(2) if the parameter 1 is yes, the execution script is displayed;
(3) Otherwise, the parameter 1 is any other value , an abnormal exit is performed;

case  "$"  in         "yes"                  echo  "Running ..."                  ;;          "quit")  echo  "quit"                  exit 0                 ;;          "*")                  echo  "Break"                  exit 1                 ;;      esac

5, write a script, complete the following functions
Pass a parameter to the script, this parameter is one of gzip, bzip2 or XZ;
(1) If the value of parameter 1 is gzip, use the TAR and gzip archive to compress/etc directories to the/backups directory and name /backups/etc-20160613.tar.gz;
(2) If the value of parameter 1 is bzip2, use tar and bzip2 archive to compress the/etc directory to the/backups directory and name it/backups/ etc-20160613.tar.bz2;
(3) If the value of parameter 1 is XZ, use the tar and XZ archives to compress the/etc directory to the/backups directory and name/backups/etc-20160613.tar.xz;
(4 ) Any other value, the error compression tool is displayed, and an abnormal exit is performed;

#!/bin/bash  if [ $# -eq 1 ];then             case $1 in                  "gzip")                      tar -zcf /backup/etc-' Date +%F '. tar.gz  /etc/* ;;                  "bzip2")                       tar -jcf /backup/etc-' date +%f ' .tar.gz /etc/* ;;                  "XZ")                       tar -jcf /backup/etc-' date +%f ' .tar.gz /etc/* ;;                 *)                       echo -e  "error pack tool \nusage: $0 gzip|bzip2|xz "                      exit  1 ;;             esac           else            echo  -e  "Parameters error \nusage: $0 gzip|bzip2|xz"          fi

6, write a script, accept a Path parameter:
(1) If it is a normal file, it can be accessed normally,
(2) If it is a directory file, you can use the CD command;
(3) If the file is a symbolic link, it is an access path;
(4) The other is impossible to judge;

#!/bin/bashif [ $# -eq 1 ];then             if [ -f $1 ];then                 echo  "$1 is file"              elif [ -d $1 ];then                 echo  "$1 is directory "             elif [ -L $1 ]; then                echo  "$1 is  symlink file"              else                echo   "Can ' t file&Nbsp;$1 type "            fi           else             echo -e  "parameter error!"           fi

7, write a script, get the host name of the current host, Judge
(1) If the host name is empty or localhost, or "(none)", then it is named Mail.magedu.com;
(2) Otherwise, the existing hostname is displayed;

#!/bin/bash          hostname=$ (CAT&NBSP;/ETC/SYSCONFIG/NETWORK&NBSP;|&NBSP;GREP&NBSP;HOSTNAME&NBSP;|&NBSP;CUT&NBSP;-D=&NBSP;-F2)          if [  "$hostname"  =  "None"  -o   "$hostname"  =  " ];then            sed -i  "S/localhost/www.magedu.com/g"   /etc/sysconfig/network          else           echo  $ (hostname)          fi 

8, write a script, accept a user name as a parameter,
(1) If the user's ID number is 0, it is displayed as an administrator;
(2) If the user's ID number is greater than 0 and less than 500, it will be displayed as a system user;
(3) Otherwise, it will be displayed as a normal user;

id $1 &> /dev/null    result=$ (id -u $1)     if [  $result  -eq 0 ];then         echo  "System admin"      elif [   $result  -ge 1  -a  $result  -le 500 ];then         echo  "System user"      else          echo  "Common user"     fi 

9, write a script, pass a user name parameter to the script;
(1) If the user's ID number is greater than or equal to 500, and its default shell is a string ending with sh, the "a user can log system" is displayed. The string for the class;
(2) Otherwise, the display cannot log on to the system;

#!/bin/bash    result=$ (id -u $1)     if [  $result  -ge 500 ];then        shell=$ (cat /etc/passwd  | grep  "$result"  | cut -d: -f7)          echo  "$shell"  | grep  "sh$"         shell_ result=$?        if [  $shell _result -eq 0 ]; then            echo  "A user can  log system. "            else             echo  "User $1 nologin system."         fi      else        echo&nbSP; " user id less than 500. "     fi

10, write a script to complete the following tasks:
(1) In order to copy each direct file or subdirectory under the/var/log directory to the/TMP/TEST1-TESTN directory,
(2) to copy the directory, use the CP-R command;
(3) Use the CP command when copying files,
(4) using the cp-d command when copying a linked file,
(5) All remaining types, using the CP-A command;

#!/bin/bash          file=/var/log/*           for i in  $file;d o                   if [ -f  $i  ];then                            echo  "$i  is common file"                             cp  $i  /tmp/test1-testn/                   elif [ -L  $i  ];then                            echo  "$i  is link file"                            cp  -d  $i  /tmp/test1-testn/                   elif [ -d  $i  ];then                            echo  "$i  is directory"                            cp  -r  $i  /tmp/test1-testn                   else                           echo  "$i  is other file"                             cp -a  $i  /tmp/test1-testn                   fi           done

This article was originally created by Marco Linux OPS students, starting with the Linux Ops tribe.


This article is from the "Marco Linux Training" blog, so be sure to keep this source http://mageedu.blog.51cto.com/4265610/1967180

10 Linux Script interview questions, see how many you can answer?

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.