【问题标题】:Bash script that creates a file with a introduced name使用引入的名称创建文件的 Bash 脚本
【发布时间】:2014-12-28 02:14:51
【问题描述】:

我想知道如何创建一个 bash 脚本,询问用户所需的名称,然后创建一个具有该名称的文件。

类似:

introduce name: test [enter]
(then use that name as the name of the final zip)
zip -9 test.zip *
(creating the file)
"test.zip" crated.
(print out that message)
end

【问题讨论】:

  • 你有没有尝试过?你看过任何“脚本入门”教程吗?

标签: linux bash terminal console


【解决方案1】:

试试这个:

read -p 'introduce name:' input
zip -9 $input.zip *

检查

help -m read | less

如果你是 初学者,一些好的开始学习指南:

常见问题解答:http://mywiki.wooledge.org/BashFAQ
导游:http://mywiki.wooledge.org/BashGuide
参考:http://www.gnu.org/software/bash/manual/bash.html
http://wiki.bash-hackers.org/
http://mywiki.wooledge.org/Quotes
检查你的脚本:http://www.shellcheck.net/

避免有人说要通过tldp.org 网站学习的建议,tldp bash 指南已经过时,在某些情况下完全是错误的。

【讨论】:

    【解决方案2】:

    我写了这样的东西

    read -p 'introduce name:' input
    ext=$(sed -n 's/\(^.[^$]*\)\(.\{3\}$\)/\2/p' <<< $input)
    if [ $ext = zip ]
        then zip -9 $input *
    elif [ $ext = cbz ]
        then zip -9 $input *
    elif [ $ext = rar ]
        then rar a $input *
    else 
        echo "I just zip, cbz and rar, sorry :("
    fi
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-22
      • 2020-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-02
      相关资源
      最近更新 更多