使用选项参数运行脚本

时间:2021-03-28 07:36:54

I want to choose with option -d a directory which I want to back up in the home directory

我想选择-d一个目录,我想在主目录中备份

I have this script:

我有这个脚本:

#!/bin/bash-

date_created="backup-`date +"%Y%m%d-%H%M"`";
backup_directory_name=$date_created;
directory="backup/";

getopts d:t: option;

case $option in
    d)
            directory=$1;
    ;;

    t)
            backup_directory_name=$1;
    ;;
esac

cd ~;
tar cvpfz $backup_directory_name.tgz $directory;

It won't run when I use an option, I get following fault:

当我使用一个选项时它不会运行,我得到以下错误:

tar: You may not specify more than one -Acdtrux or --test-label option Try tar --help' ortar --usage' for more information.

tar:您可能没有指定多个-Acdtrux或--test-label选项尝试tar --help'ortar --usage'以获取更多信息。

1 个解决方案

#1


1  

You probably need to re-write it as a loop around getopts():

您可能需要将其重新编写为getopts()的循环:

while getopts "..." opt ; do
    case $opt in
    ...
done

shift $((OPTIND-1))

#1


1  

You probably need to re-write it as a loop around getopts():

您可能需要将其重新编写为getopts()的循环:

while getopts "..." opt ; do
    case $opt in
    ...
done

shift $((OPTIND-1))