欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Linux下复制命令的脚本

程序员文章站 2022-04-05 23:03:05
Linux下复制命令的脚本在制作linux小系统时,我们需要复制需要用到的命令到指定目录,同时包括一些命令所依赖的库文件[root@wxxdc-sys-ilodhcp01 ~]# which ls #二进制命令所在路径alias ls='ls --color=auto' /bin/ls[root@wxxdc-sys-ilodhcp01 ~]# ldd /bin/ls #ldd命令查看命令所依赖的库文件 linux-vdso.so.1 => ......

Linux下复制命令的脚本

在制作Linux小系统时,我们需要复制需要用到的命令到指定目录,同时包括一些命令所依赖的库文件,整个复制工作十分繁琐,这里我们使用脚本来简化我们的操作

[root@wxxdc-sys-ilodhcp01 ~]# which ls    #二进制命令所在路径
alias ls='ls --color=auto'
        /bin/ls

[root@wxxdc-sys-ilodhcp01 ~]#  ldd /bin/ls    #ldd命令查看命令所依赖的库文件
        linux-vdso.so.1 =>  (0x00007ffe60bda000)
        libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f72b37b4000)
        libcap.so.2 => /lib64/libcap.so.2 (0x00007f72b35af000)
        libacl.so.1 => /lib64/libacl.so.1 (0x00007f72b33a5000)
        libc.so.6 => /lib64/libc.so.6 (0x00007f72b2fd8000)
        libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f72b2d76000)
        libdl.so.2 => /lib64/libdl.so.2 (0x00007f72b2b71000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f72b39e8000)
        libattr.so.1 => /lib64/libattr.so.1 (0x00007f72b296c000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f72b2750000)

脚本


#!/bin/bash
DEST=/mnt/sysroot
libcp() {
        LIBPATH=${1%/*}
        [ ! -d $DEST$LIBPATH ] && mkdir -p $DEST$LIBPATH 
        [ ! -e $DEST${1} ] && cp $1 $DEST$LIBPATH && echo "copy lib $1 finished"
}
bin_cp() {
        CMDPATH=${1%/*}
        [ ! -d $DEST$CMDPATH ] && mkdir -p $DEST$CMDPATH
        [ ! -e $DEST${1} ] && cp $1 $DEST$CMDPATH
        
        for i in `ldd $1 | grep -o "/[^[:space:]]*"`; do
              libcp $i
        done
}
read -p "please input your commad: " CMD
until [ $CMD = 'q' ];do
        ! which $CMD && echo "Wrong command" && read -p "please input again" CMD && continue
        COMMAND=`which $CMD | grep -v "^alias" | grep -o "/.*"`
        bin_cp $COMMAND
        echo "copy $COMMAND finished"
        read -p "Continue: " CMD
done

脚本中关于截取命令路径的表达式讲解如下

man bash  #截取目录

${parameter#word}
       ${parameter##word}
              Remove  matching  prefix pattern.  The word is expanded to produce a pattern just as in pathname expansion.  If
              the pattern matches the beginning of the value of parameter, then the result of the expansion is  the  expanded
              value  of  parameter  with  the shortest matching pattern (the ``#'' case) or the longest matching pattern (the
              ``##'' case) deleted.  If parameter is @ or *, the pattern removal operation  is  applied  to  each  positional
              parameter in turn, and the expansion is the resultant list.  If parameter is an array variable subscripted with
              @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is  the
              resultant list.

${parameter%word}
       ${parameter%%word}
              Remove  matching  suffix pattern.  The word is expanded to produce a pattern just as in pathname expansion.  If
              the pattern matches a trailing portion of the expanded value of parameter, then the result of the expansion  is
              the  expanded  value  of  parameter with the shortest matching pattern (the ``%'' case) or the longest matching
              pattern (the ``%%'' case) deleted.  If parameter is @ or *, the pattern removal operation is  applied  to  each
              positional  parameter in turn, and the expansion is the resultant list.  If parameter is an array variable sub‐
              scripted with @ or *, the pattern removal operation is applied to each member of the array  in  turn,  and  the
              expansion is the resultant list.

例子:

FILE=/usr/local/src

echo ${FILE#*/}    一个#号表示从左往右找到第一个/往左的所有字符删除

Linux下复制命令的脚本

echo ${FILE##*/}            两个#号表示从左往右找到最后一个/往左的所有字符删除

Linux下复制命令的脚本

echo ${FILE%/*}           一个%号表示从右往左找到第一个/往右的所有字符删除

Linux下复制命令的脚本

echo ${FILE%%/*}       两个%号表示从右往左找到最后一个/往右的所有字符删除

Linux下复制命令的脚本

 

 

本文地址:https://blog.csdn.net/owlcity123/article/details/107335279