shell脚本中实现脚本a里面的函数调用脚本b中的函数参数-用到source关键字

来源:互联网 发布:阿里云先知大会 编辑:程序博客网 时间:2024/06/08 17:53

##########################################################

这里就以常用的拷贝压缩为例进行实战

##########################################################

首先先写好option.sh脚本的功能

[root@localhost shell]# cat option.sh
#!/bin/bash
delete()
{   
    rm -rf $del
}
copy()
{
    cp -rf $dir $tdir

}   
backup()
{
    tar zcvf $tar_name $tar_dir &> /dev/null
}
quit()
{
    exit
}
第二步、在file.sh脚本中调用option.sh脚本中的参数

[root@localhost shell]# cat file.sh 
#!/bin/bash
source /shell/option.sh
while true
do
cat <<EOF
**************************************
   the following is  optional        *
                                     *
**************************************
          1) Copy                    *
          2) Delete                  *
          3) Backup                  *
          4) Exit                    *
**************************************
EOF
read -p "please enter your options:" option


case $option in
1)
read -p "please input your want to copy the source file :" sdir
read -p "please input your target directory:" tdir
copy
;;


        2)
        read -p "Please input your target directory:" del
        delete
        ;;


        3)
        read -p "please enter your backup file names:" tar_name
        read -p "please enter your backup file:" tar_dir
        backup
        ;;


         4)
        quit
        break
        ;;


        *)
          echo "$option Is not optional operation"

esac
done

0 0