Qingming Three days holiday basically is spent in writing scripts, today again toss a new script, the role of the script is to quickly copy one or more of the command executable files and dependent library files into a simulated root file system under the corresponding directory, the script is usually not used in operation and maintenance estimates, It is only possible to use this script if you make a small Linux distribution yourself.
The script specifically features the following:
1) Prompts the user to select whether to read the command name to be copied from the text or interactively enter the command name from the current terminal.
2) The user chooses the former, will automatically use VIM to open a file, the user according to the format instructions to fill in the name of the command to be copied, can be more than one command, save to automatically perform the copy operation after exiting.
3) The user chooses the latter, interactively enter the name of the single command in the terminal, and then perform the copy operation after carriage return.
Note: If the destination directory does not have a corresponding subdirectory, it is created automatically, such as the/bin corresponding/tmp/sysroot/bin.
The specific implementation of the script as follows (Slag slag English, Baidu translation, Please forgive me):
#!/bin/bash#program:# this program is used to cpoy executable file and dependency Library # to the specified folder#history: #2016/4/4 xiaohei v1.0#blog:http://zww577593841.blog.51cto.com/6145493/1750689#lang=zh_cn. utf8path=./: $PATHexport path# The name of the command to copy declare cmd# the executable path of the command declare cmdfile# dependent on the path of the library file declare libfile# the target directory to copy to declare sysroot= "/tmp/sysroot" #信号捕捉trap ' Mytrap ' intmytrap () { clean_temp exit 1} #清理脚本运行过程中生成的临时文件和变量clean_temp () { unset -v cmd unset -v cmdfile unset -v libfile &nBsp; unset -v sysroot rm -rf ./libfile.txt rm -rf ./cmdlist.txt rm -rf ./cmdfile.txt rm -rf ./cmdlist.txt.bak} #判断是否输入一个参数, the input parameter is not a command, the executable file for the command exists Analyse_cmd () { if [ $# -ne 1 ];then echo -e "\033[ 31mthe parameter error\033[0m " return 1 fi if ! type "$" &> /dev/null; then echo -e "\033[31mnot a command\033[0m " return 1 fi if ! which "$" &> /dev/null ;then echo -e "\033[ 31mcmdfile does not exist\033[0m " return 1 fi return 0}analyse_dir_file () { #判断要复制的文件是否存在 if ! [ -a $ 1 ];then echo -e "\033[31m$1 does not exist\033[0m " return 1 fi #取文件路径名 local dir_file=${1%/*} [ -d $dir _file ] #如果目录不存在则创建 if ! [ -d $sysroot $dir_file ];then mkdir -p $sysroot $dir_file echo -e "\033[ 32mcreat $sysroot $dir_file\033[0m " return 0 fi # If the file to which you want to copy already exists, error if [ -a $sysroot ];then echo -e "\033[31m$sysroot$1 has exist\033[0m" return 1 fi return 0}cp_file () { #调用函数分析给定的参数 analyse_cmd $1 | | return 1 #获取命令的可执行文件的路径 cmdfile=$ (which $1) echo "Cmdfile is: $cmdfile" #调用函数分析获取的路径和文件, normal copy, exception Exit Function analyse_dir_file $cmdfile && cp -a "$cmdfile" "$sysroot $cmdfile" | | return 1 echo -e "\033[32mcp $ cmdfile successful\033[0m " #获取命令的依赖库文件名 and save to file ldd $cmdfile | grep -o -E "/[^[:space:]]+" > ./libfile.txt #循环遍历文件, Content is the dependent library file name found for the previous command while read libfile ;d o #对库文件的绝对路径名进行分析, perform the next step normally, and exit the loop unexpectedly. analyse_dir_ file $libfile | | continue #复制文件到目标路径下 &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CP "$libfile" "$sysroot $libfile" echo -e "\033[32mcp $libfile successful\ 033[0m " done < ./libfile.txt}# Manually add more than one command name to a file in a fixed format cmdlist_creat () { #判断文件是否存在, there is a renaming of it if [ -f ./cmdlist.txt ];then Mv -f ./cmdlist.txt ./cmdlist.txt.bak fi #给文件添加格式说明 cat > ./cmdlist.txt <<end#Each line is a command #ls #pwd end #编辑文件, add the name of the command you want to copy vim ./cmdlist.txt} #遍历文件, which saves the name of the command to be copied, copies each command's executable file and the dependent library to the appropriate directory in the specified directory Cp_file_ Text () { #局部变量, save command name local cmdf #循环遍历文件, find the command that does not begin with the # number, call the function to perform the copy operation while read cmdf ;d o #排查格式说明的举例 if [[ "$cmdf" =~ ' # ' ]];then continue fi echo "$cmdf" cp_file $cmdf done < ./cmdlist.txt}while true ;d ocat <<end##################################### #a) Read from the text &NBSP;&NBSP;#&NBSP;&NBSP;B) read from the terminal #quit) quit ###################################### endread -p "Enter you choice:" optioncase $option in[aa]) cmdlist_creat cp_file_text ;; [BB]) read -p "Enter a cmd:" cmd cp_file $cmd ;; Quit) break ;; *) continue ;; esacdone# Cleanup Script-generated temporary files and variables Clean_temp
Program Run Test:
1) Script Run effect
650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M00/7E/7E/wKiom1cCQKuiIwokAAA0fjz-fEA319.png "title=" image 128. PNG "alt=" Wkiom1ccqkuiiwokaaa0fjz-fea319.png "/>
2) Select the command to copy from text
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/7E/7E/wKiom1cCQQKjArQuAAAoxVW7ZDc376.png "title=" image 129. PNG "alt=" Wkiom1ccqqkjarquaaaoxvw7zdc376.png "/>
650) this.width=650; "src=" http://s4.51cto.com/wyfs02/M00/7E/7B/wKioL1cCQcujKuHtAABg5Fz3t7o190.png "title=" image 138. PNG "alt=" Wkiol1ccqcujkuhtaabg5fz3t7o190.png "/>
3) Select interactive input from terminal to add the command
650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M01/7E/7E/wKiom1cCQkHDeCG8AABAWcTNIbs300.png "title=" image 135. PNG "alt=" Wkiom1ccqkhdecg8aabawctnibs300.png "/>
4) See if you are actually copying
650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M00/7E/7B/wKioL1cCRICQJxB9AABewKVS05Y197.png "title=" image 139. PNG "alt=" Wkiol1ccricqjxb9aabewkvs05y197.png "/>
5) test the error information of various errors input, note that the script can only recognize external commands, bash built-in command will be error.
650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M00/7E/7B/wKioL1cCRUOxvFsJAAA-IwvMZig407.png "title=" image 140. PNG "alt=" Wkiol1ccruoxvfsjaaa-iwvmzig407.png "/>
650) this.width=650; "src=" http://s5.51cto.com/wyfs02/M01/7E/7E/wKiom1cCRRXAeUw3AAA6ol02X4o660.png "title=" image 141. PNG "alt=" Wkiom1ccrrxaeuw3aaa6ol02x4o660.png "/>
This script is basically for practice, is for the post-production of Linux small System Preparation Tool script.
This article is from the "Little Black" blog, please be sure to keep this source http://zww577593841.blog.51cto.com/6145493/1760198
Small black Daily Toss-Copy the executable file of the external command and the corresponding directory of the dependent library file to the specified directory