Linux Operations and Maintenance - shell script exercise string burning 1

Posted by e-novative on Thu, 29 Aug 2019 10:22:59 +0200

Script description
1. Script 1: Solution to reload a large number of library files
2. Script 2: Select and switch to the working directory you want to switch

Script 1 explains:

One employee's centos7 host system had the following problems:

implement ldconfig The following error occurred in command time: the library files are empty and the length is 0. These library files need to be reloaded.               
ldconfig: File /lib/libstdc++.so.6 is empty, not checked.
ldconfig: File /lib/libstdc++.so.6.0.19 is empty, not checked.
ldconfig: File /lib/libaudit.so.1 is empty, not checked.
ldconfig: File /lib/libaudit.so.1.0.0 is empty, not checked.
ldconfig: File /lib/libauparse.so.0 is empty, not checked.
ldconfig: File /lib/libauparse.so.0.0.0 is empty, not checked.
ldconfig: File /lib/libdw-0.172.so is empty, not checked.
ldconfig: File /lib/liblz4.so.1 is empty, not checked.
ldconfig: File /lib/libelf-0.172.so is empty, not checked.
ldconfig: File /lib/libasm-0.172.so is empty, not checked.
ldconfig: File /lib/liblz4.so.1.7.5 is empty, not checked.
ldconfig: File /lib64/libXfont.so.1 is empty, not checked.
ldconfig: File /lib64/libXfont.so.1.4.1 is empty, not checked.
ldconfig: File /lib64/libical.so.1 is empty, not checked.
ldconfig: File /lib64/libical.so.1.0.1 is empty, not checked.
ldconfig: File /lib64/libicalss.so.1 is empty, not checked.
ldconfig: File /lib64/libicalss.so.1.0.1 is empty, not checked.
ldconfig: File /lib64/libicalvcal.so.1 is empty, not checked.
ldconfig: File /lib64/libicalvcal.so.1.0.1 is empty, not checked.
ldconfig: File /lib64/libminizip.so.1 is empty, not checked.
ldconfig: File /lib64/libminizip.so.1.0.0 is empty, not checked.
ldconfig: File /lib64/libgit2-glib-1.0.so.0 is empty, not checked.
ldconfig: File /lib64/libgit2-glib-1.0.so.0.2600.4 is empty, not checked.
ldconfig: File /lib64/libopenconnect.so.5.4.0 is empty, not checked.
ldconfig: File /lib64/libnghttp2.so.14.16.1 is empty, not checked.
.............ellipsis

As shown above: These library files need to be re-loaded, but not every line of information shows the library files, some lines are soft-link files, because the linked library files are empty, so the soft-link files are empty.

The idea of script 1
1. First, you need to intercept the full path and name of the library file, such as (/lib64/libical.so).
2. Through the rpm-qf library file name, we can find out which package the library file belongs to, and then reinstall the package.
3. It is necessary to judge the soft-link file. If it is a link file, the reinstallation command will not be executed.
4. Use a loop to traverse the list of Library files. Here you use a while loop.

Content of script 1

#/bin/bash
LDCONFIG=`ldconfig 2>&1|sort|awk '{print $3}'`        #The name of the library file is intercepted because the ldconfig command outputs the error output message, so the error output is redirected to the standard output with 2 > & 1, so the command after the pipeline can accept the information.

echo "$LDCONFIG" |           #Here we use pipelines to let while loop through the list of Library files, noting that double quotation marks are added.
while read line                      #while read line classical combination
 do
   if [ `ls -l $line | grep -c '^l'` -ne 0 ]         #To determine whether a file is a link file, grep-c counts the number of occurrences of matching strings. If a link file is a link file, grep-c'^ l'is 1, 1 is not equal to 0. Execute the continue statement and jump out of the current cycle.
     then
         continue
   else
         PACKAGE=`rpm -qf $line`             #If it is a library file, query which package the library file belongs to
         if [ `echo "$PACKAGE" | wc -l` -eq 1 ]   #Determine whether a library file belongs to only one package, and if there is only one, reinstall the package
           then
                echo "prepare reinstall $PACKAGE"
                yes | yum reinstall $PACKAGE        #The yes command always outputs y if it doesn't follow the parameters. Why not use yum-y install? Because it's boring.
                [ $? -eq 0 ] && echo "$PACKAGE reinstall succes" || echo "$PACKAGE reinstall failed"  
           else  
                echo 'I don not know need reinstall which PACKAGE'               
         fi
   fi
 done

Summary of script 1
The script does not record log information and other information such as installation errors. It simply satisfies the need to reload a large number of Library files, which can not be reloaded alone.

Script 2 Description

In my work, I often switch to some directories frequently. Just after reading the introduction of pushd, popd, dirs and other commands, I want to write a script that uses these commands to switch directories. The command instructions used in the script are as follows:

1.pushd command: shell built-in command, pushd command with the directory name as a parameter can be added to the directory stack. If the parameter is + n (where n is a number), pushd will rotate the stack, and the nth directory in the stack will be placed on the top of the stack from the left, and switch to that directory at the same time. (Content from Unix shell exemplar refinement fourth edition)

2.popd command: shell built-in command, popd command deletes a directory from the top of the stack and switches to that directory. ... (Content from Unix shell exemplar refinement fourth edition)

3.dirs command: The built-in command dirs with the - l option will display all directories in the directory stack in full path name format. (Content from Unix shell exemplar refinement fourth edition)

The idea of script 2

1. Make a menu of directories with select and switch to the directory if you choose which directory.
2. It is necessary to add directories to the directory stack by pushd and pop directories out of the directory stack by popd command.
3. The directory in the directory stack and the directory number need to be exemplified with the dirs command.

Content of script 2

This is a function written in the / etc/profile file. At first, we write the script file separately and execute the script. We find that there is no change of directory. It should be a new process when we execute the script. Therefore, when the current process does not change directory, there is no further test of the script, we write it as a function and put it in / etc./ In the profile file, the contents are as follows:

function cdir ()       #Function name cdir
{
DIRS="                  #This variable stores a list of directories
/home/kevin.tao/git-server/puppet/modules/nagios/files/client/centos7
/home/kevin.tao/git-server/puppet/modules/nagios/files/server/objects
/home/kevin.tao/git-server/puppet/modules/db_backup
/home/kevin.tao/git-server/puppet/manifests
/home/kevin.tao/Desktop"
set $DIRS             #The set command can reassign the location parameter, where the directory name is changed to $1 $2 $3 $4.....
while popd ; do : ; done &>/dev/null    #The while loop condition is the popd command, which empties the directory stack. If not, every time a function is executed, the directory is added to the directory stack repeatedly, which is not correct.
until [ -z $1 ]          #The until loop is used to add directories to the directory stack. Note the shift command
do
   pushd $1 &>/dev/null
   shift
done
PUSH=`dirs -l -v|sed '$d'|sed 's/ //G'`PUSH variable is used to store directory list and directory number (n) in directory stack.
PS3="Please choose directory"           
select DIR in $PUSH                            #Select menu, select the directory to switch
do
    NUM=${DIR%%/*}               #Which directory is selected, and the directory number of the directory in the stack is intercepted here
    pushd +$NUM &>/dev/null       #pushd command with directory number can switch to that directory
    break
done 
}

export -f cdir

Script 2 execution

[kevin.tao@cws76 ~]$ cdir 
1) 0/home/kevin.tao/Desktop
2) 1/home/kevin.tao/git-server/puppet/manifests
3) 2/home/kevin.tao/git-server/puppet/modules/db_backup
4) 3/home/kevin.tao/git-server/puppet/modules/nagios/files/server/objects
5) 4/home/kevin.tao/git-server/puppet/modules/nagios/files/client/centos7
Please choose directory2
[kevin.tao@cws76 manifests]$     #Successfully switch to the selected directory

Summary of script 2

Writing scripts will encounter many problems, which need to be patched slowly. At first I didn't think popd command was needed. Later I found that there were too many duplicate directories in the directory stack and only one directory popped up. If you use the cd command, it will be much easier to estimate, but you haven't tried it. You want to try some different methods and techniques to improve the ability of shell programming.

Topics: Linux git shell Unix RPM