码迷,mamicode.com
首页 > 系统相关 > 详细

copy命令及库文件至/mnt/sysroot/目录shell脚本

时间:2017-05-27 23:53:42      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:copy 命令 shell脚本

#!/bin/bash
#

#获取目标目录
target=/mnt/sysroot

#命令copy函数
cmndcopy(){
        #假如命令不存在,return 1
        if ! which $1 &> /dev/null; then
                return 1
        fi
        #获取命令决定路径
        cmnd=$(which --skip-alias $1)
        #获取命令目录名
        cmndpath=$(dirname $cmnd)
        
        #判断命令目录名是否存在,否,则创建
        [ -d $target/$cmndpath ] || mkdir -p $target/$cmndpath
        
        #判断命令是否存在,否,则copy
        [ -e $target/$cmnd ] || cp $cmnd $target/$cmnd
        return 0
}

#库文件copy函数
libcopy(){
        #获取库文件目录名
        libpath=$(dirname $1)
        
        #判断库文件目录名是否存在,否,则创建
        [ -d $target/$libpath ] || mkdir -p $target/$libpath
        
        #判断库文件是否存在,否,则copy
        [ -e $target/$1 ] || cp $1 $target/$1
}

#提示输入要copy的命令,quit可以退出
read -p "Plz enter a command(quit for quiting):" binary

#循环copy命令,遇到quit字符推出循环
until [ $binary == "quit" ]; do
        
        #调用命令copy函数 
        cmndcopy $binary
        
        #保存命令copy函数返回值
        retval=$?
        
        #如果返回值等于0,则
        if [ $retval -eq 0 ]; then
            
                #获取命令库文件的路径
                binary=$(which --skip-alias $binary)
                
                #循环取出库文件路径
                for lib in $(ldd $binary | grep -o "/[^[:space:]]*lib[^[:space:]]*"); do
                        #调用库文件copy函数
                        libcopy $lib
                done
        
        #如果返回值不为0,则
        else
                #提示命令输入错误,并可以重新输入
                read -p "$binary is not correct, Plz enter again(quit for quiting):" binary
                #并提前结束本轮循环
                continue
        fi
        
        #再次提示输入要copy的命令,quit可以退出
        read -p "Plz enter a command(quit for quiting):" binary
done


本文出自 “WangJian的IT之路!” 博客,请务必保留此出处http://asherwang.blog.51cto.com/11255017/1930334

copy命令及库文件至/mnt/sysroot/目录shell脚本

标签:copy 命令 shell脚本

原文地址:http://asherwang.blog.51cto.com/11255017/1930334

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!