Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 用参数替换bash脚本中的部分命令_Linux_Bash_Shell_Command Line_Arguments - Fatal编程技术网

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

Linux 用参数替换bash脚本中的部分命令,linux,bash,shell,command-line,arguments,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-l en

我对编程非常陌生,尤其是在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-l en_US.UTF-8 startdisk multicast_restore nvme0n1

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

其中,“arga”是替换上述字符串中
的数字,“argb”替换


谢谢你的帮助

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

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

将其放入文件中,并将
更改为
“$1”
更改为
“$2”
?@123,您应该添加一个answer@glennjackman我相信一定有重复的地方。只是一个注意:在许多情况下,你可以使用一个shell函数,而不是麻烦的脚本文件;但是不能在shell函数上使用
sudo
,因此在本例中,脚本是一种方法。