linux - 用参数替换 bash 脚本中的部分命令

标签 linux bash shell command-line arguments

我对编程非常陌生,尤其是在 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>在上面的字符串中。

感谢您的帮助!

最佳答案

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

#!/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

关于linux - 用参数替换 bash 脚本中的部分命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41988764/

相关文章:

linux - 从 linux 中的多个文件名中删除 "-template"部分

linux - 为什么 "su - -c"会导致失败?

linux - 来源与执行脚本 : in which shell process do they run?

shell - 在 HDFS 中查找文件的行数以与 QC 文件中的行数进行比较

django - 安装pipenv导致pip3无法使用

linux - 连接到与我无法控制的路由器相连的 Raspberry PI

linux - smbclient : command not found

bash - 如何在 Makefile 中使用 shell 命令

bash - 使用 awk 或 bash 将字符串附加到列中单元格的所有元素

linux - 使用 bash $ 命令执行 vi