Linux下复制命令的脚本

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#*/}    一个#号表示从左往右找到第一个/往左的所有字符删除

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

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

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值