【问题标题】:substitute parts of a command in a bash script with arguments用参数替换 bash 脚本中命令的一部分
【发布时间】:2017-06-18 17:31:44
【问题描述】:

我对编程很陌生,尤其是 bash。

我经常运行相同的长命令,每天填充许多参数,但使用不同的文件名和一个其他参数,字符串的大部分保持不变。 Clonezilla 服务器就是这种情况。

我希望能够运行一个脚本,它只询问我想要更改的部分,然后将这些值替换为原始字符串并将其传递给 shell。

例如:

我有一组文件名:a.img b.img c.img

我运行的命令:drbl-ocs -b -g auto -e1 auto -e2 -r -x -j2 -sc0 -p poweroff --clients-to-wait <number value here> -l en_US.UTF-8 startdisk multicast_restore <name of file> nvme0n1

我的目标是能够输入:sudo sh myscript.sh arga argb

其中“arga”是一个数字,用于替换上述字符串中的<number value here>,而“argb”则是替换<name of file>

感谢您的帮助!

【问题讨论】:

  • 把它放到一个文件里,把<number value here>改成"$1",把<name of file>改成"$2"?
  • @123,你应该添加一个答案
  • @glennjackman 我确定某处一定有重复。

标签: linux bash shell command-line arguments


【解决方案1】:

脚本的命令行参数可以在位置参数中找到,参数名称为正整数。

#!/bin/sh

drbl-ocs -b -g auto -e1 auto -e2 -r -x -j2 -sc0 \
         -p poweroff --clients-to-wait "$1" -l en_US.UTF-8 \
         startdisk multicast_restore "$2" nvme0n1

【讨论】:

  • 请注意:在许多情况下,您可以使用 shell 函数而不是使用脚本文件;但是你不能在 shell 函数上使用sudo,所以在这种情况下,脚本是要走的路。
猜你喜欢
  • 2010-11-29
  • 1970-01-01
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
  • 2014-08-23
  • 1970-01-01
  • 1970-01-01
  • 2015-03-21
相关资源
最近更新 更多