是否可以在第一个提示中提供多个要执行的变量?在第一个提示中,我想提供 1 个或 2 个(最多 5 个)在一个会话中执行特定脚本的变量。
例如:
echo -n "Enter the script you want to launch and press [ENTER]
read script
相反$script
,我希望有一些替代方法,例如读取脚本 1 或脚本 2 ... 直到脚本 5,这使我能够使用这个单个条目启动任意数量的脚本 #i 想要的,并以任意组合(例如 1 和 2、1 和 3 和 5 等)
echo "Hello, "$USER". This script will allow you to start $
echo "1. Claymore ZEC v12.6"
echo "2. Claymore ETH & SIA v10"
echo "3. Claymore ETH v10"
echo "4. XMRIG ETN v2.4.4 //etn-pool.proxpool"
echo "5. XMRIG ETN v2.4.4 //pool.etn.spacepools.org"
echo "6. Quit"
echo -n "Enter the script you want to launch and press [ENT$
read script
case $script in
1)
echo "Starting to mine ZEC."
cd ~/Downloads/claymore.s.Zcash.v12.6/;
./mine.sh;
;;
2)
echo "Starting to mine ETH & SIA."
cd ~/Downloads/claymore.dualminer.v10_ETHSIA/;
./mine.sh;
;;
3)
echo "Starting to mine ETH."
cd ~/Downloads/claymore.dualminer.v10_ETHSIA/;
./mineETH.sh;
;;
4)
echo "Starting to mine ETN."
cd ~/Downloads/Electroneum/XMRIG/build/;
sudo sysctl -w vm.nr_hugepages=128;
clear;
cat /proc/meminfo | grep Huge;
./mine.sh;
;;
5)
echo "Starting to mine ETN."
cd ~/Downloads/Electroneum/XMRIG/build/;
sudo sysctl -w vm.nr_hugepages=128;
clear;
cat /proc/meminfo | grep Huge;
./mine_space.sh;
;;
6)
echo "Quit"
;;
esac
答案1
我根据 @steeldriver 留下的评论为您整理了一个。这只是一个命令行脚本,允许在命令行上输入多个内容。它用于getopts
多个选项,并添加了nohup command &
将脚本移至后台以便运行下一个案例的功能。
如需进一步帮助,请在互联网上搜索 bash getopts。有很多东西需要学习,您可以编写出色的脚本来做任何您想做的事情。
剧本:
#!/bin/bash
#Set name
NAME=$(basename $0)
#Set option choices
OPTS="h12345"
PUSAGE=""
#This is how to use the script
usage="
Hello, "$USER". This script will allow you to start the following.
You can run as many as you like starting with a `-`. Example below.
Usage: ${NAME} [OPTIONS]
Options are:
-h Show this message.
-1. Claymore ZEC v12.6
-2. Claymore ETH & SIA v10
-3. Claymore ETH v10
-4. XMRIG ETN v2.4.4 //etn-pool.proxpool
-5. XMRIG ETN v2.4.4 //pool.etn.spacepools.org
Example:
${NAME} -135
"
#Run script
while getopts :${OPTS} i ; do
case $i in
1)
echo "Starting to mine ZEC."
cd ~/Downloads/claymore.s.Zcash.v12.6/
nohup ./mine.sh & 2>&1>/dev/null;;
2)
echo "Starting to mine ETH & SIA."
cd ~/Downloads/claymore.dualminer.v10_ETHSIA/
nohup ./mine.sh & 2>&1>/dev/null;;
3)
echo "Starting to mine ETH."
cd ~/Downloads/claymore.dualminer.v10_ETHSIA/
nohup ./mineETH.sh & 2>&1>/dev/null;;
4)
echo "Starting to mine ETN."
cd ~/Downloads/Electroneum/XMRIG/build/
sudo sysctl -w vm.nr_hugepages=128
clear
cat /proc/meminfo | grep Huge
nohup ./mine.sh & 2>&1>/dev/null;;
5)
echo "Starting to mine ETN."
cd ~/Downloads/Electroneum/XMRIG/build/
sudo sysctl -w vm.nr_hugepages=128
clear
cat /proc/meminfo | grep Huge
nohup ./mine_space.sh & 2>&1>/dev/null;;
h | \?) PUSAGE=1;;
esac
done
#Show help based on selection
if [ ${PUSAGE} ]; then
echo "${usage}"
exit 0
fi
#Check for input if none show help.
if [[ $1 == "" ]]; then
echo "${usage}"
exit 0
fi
现在,如果您运行该脚本,后面不加任何内容或不加任何-h
帮助,则会出现以下内容。我命名我的脚本askhelp.bsh
只是为了举例。
~$ ./askhelp.bsh
Hello, terrance. This script will allow you to start the following.
You can run as many as you like separated by spaces and dashes. Example below.
Usage: askhelp.bsh [OPTIONS]
Options are:
-h Show this message.
-1. Claymore ZEC v12.6
-2. Claymore ETH & SIA v10
-3. Claymore ETH v10
-4. XMRIG ETN v2.4.4 //etn-pool.proxpool
-5. XMRIG ETN v2.4.4 //pool.etn.spacepools.org
Example:
askhelp.bsh -135
然后在我的测试脚本中我使其回显这些行以表明它们正在运行。
例子:
~$ ./askhelp.bsh -135
Starting Option 1
Starting to mine ZEC.
cd ~/Downloads/claymore.s.Zcash.v12.6/
nohup ./mine.sh & 2>&1>/dev/null
Starting Option 3
Starting to mine ETH.
cd ~/Downloads/claymore.dualminer.v10_ETHSIA/
nohup ./mineETH.sh & 2>&1>/dev/null
Starting Option 5
Starting to mine ETN.
cd ~/Downloads/Electroneum/XMRIG/build/
sudo sysctl -w vm.nr_hugepages=128
clear
cat /proc/meminfo | grep Huge
nohup ./mine_space.sh & 2>&1>/dev/null
另一个例子:
~$ ./askhelp.bsh -24
Starting Option 2
Starting to mine ETH & SIA.
cd ~/Downloads/claymore.dualminer.v10_ETHSIA/
nohup ./mine.sh & 2>&1>/dev/null
Starting Option 4
Starting to mine ETN.
cd ~/Downloads/Electroneum/XMRIG/build/
sudo sysctl -w vm.nr_hugepages=128
clear
cat /proc/meminfo | grep Huge
nohup ./mine.sh & 2>&1>/dev/null
根据请求的新脚本:
此脚本应gnome-terminal
为每个回溯选择打开一个新终端。命令已为 完成gnome-terminal
。如果您决定使用不同的终端,请确保对每个 case 语句中的命令进行适当的更改。
#!/bin/bash
#Set name
NAME=$(basename $0)
#Set option choices
OPTS="h12345"
PUSAGE=""
#This is how to use the script
usage="
Hello, "$USER". This script will allow you to start the following.
You can run as many as you like starting with a `-`. Example below.
Usage: ${NAME} [OPTIONS]
Options are:
-h Show this message.
-1. Claymore ZEC v12.6
-2. Claymore ETH & SIA v10
-3. Claymore ETH v10
-4. XMRIG ETN v2.4.4 //etn-pool.proxpool
-5. XMRIG ETN v2.4.4 //pool.etn.spacepools.org
Example:
${NAME} -135
"
#Run script
while getopts :${OPTS} i ; do
case $i in
1)
gnome-terminal -x bash -c 'echo "Starting to mine ZEC."; cd ~/Downloads/claymore.s.Zcash.v12.6/; ./mine.sh';;
2)
gnome-terminal -x bash -c 'echo "Starting to mine ETH & SIA."; cd ~/Downloads/claymore.dualminer.v10_ETHSIA/; ./mine.sh';;
3)
gnome-terminal -x bash -c 'echo "Starting to mine ETH."; cd ~/Downloads/claymore.dualminer.v10_ETHSIA/; ./mineETH.sh';;
4)
gnome-terminal -x bash -c 'echo "Starting to mine ETN."; cd ~/Downloads/Electroneum/XMRIG/build/; sudo sysctl -w vm.nr_hugepages=128; clear; cat /proc/meminfo | grep Huge; ./mine.sh';;
5)
gnome-terminal -x bash -c 'echo "Starting to mine ETN."; cd ~/Downloads/Electroneum/XMRIG/build/; sudo sysctl -w vm.nr_hugepages=128; clear; cat /proc/meminfo | grep Huge; ./mine_space.sh';;
h | \?) PUSAGE=1;;
esac
done
#Show help based on selection
if [ ${PUSAGE} ]; then
echo "${usage}"
exit 0
fi
#Check for input if none show help.
if [[ $1 == "" ]]; then
echo "${usage}"
exit 0
fi
希望这可以帮助!
答案2
您可以使用bash
read
命令从用户那里获取几个参数。
下面的例子取自bash 输入教程
下面的脚本在一个read
命令中读取 3 个输入参数,并在脚本的后面使用这 3 个唯一参数。
cars.sh 脚本
#!/bin/bash
# Demonstrate how read actually works
echo What cars do you like?
read car1 car2 car3
echo Your first car was: $car1
echo Your second car was: $car2
echo Your third car was: $car3
终端执行:
./cars.sh
What cars do you like?
Jaguar Maserati Bentley
Your first car was: Jaguar
Your second car was: Maserati
Your third car was: Bentley
./cars.sh
What cars do you like?
Jaguar Maserati Bentley Lotus
Your first car was: Jaguar
Your second car was: Maserati
Your third car was: Bentley Lotus