3

Is it possible to provide more than one variable to be executed in first prompt? at first prompt I would like to provide 1 or 2 up to 5 variables which are executing particular script within one session.

e.g.:

echo -n "Enter the script you want to launch and press [ENTER]
read script

Instead of $script, I would like to have some alternative like reading script1 or script2 ... up to script5, which allows me to execute with this single entry launch any number of script #i want, and at any combination (eg 1 and 2, 1 and 3 and 5, etc)

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
Kevin Bowen
  • 19,615
  • 55
  • 79
  • 83
Harry Keogh
  • 33
  • 1
  • 6

2 Answers2

1

You can use bash read command in order to get several parameters from the user.

The below example was taken from bash input tutorial

The below script read 3 input parameters in one read command and use those 3 unique parameters later in the script.

cars.sh script

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

Terminal execution:

./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
Yaron
  • 13,173
  • but you know sometimes I am just giving one option smetimes 3 or 4, I would like the script to execute all relevant options within one terminal session :) – Harry Keogh Jan 22 '18 at 13:22
  • @HarryKeogh - The issue raised in your comment, is a new issue which wasn't mentioned in your question. If you want answer for it - please [edit] your question (or ask a new question) and specify the exact problem you are facing, with some examples - how would you like to use it, etc. – Yaron Jan 22 '18 at 13:25
  • as per my taste this is still the same case, at first prompt I would like to provide 1 or 2 up to 5 variables which are executing particular script within one session. – Harry Keogh Jan 22 '18 at 13:29
  • @HarryKeogh - if you want to get an answer which will solve your question, you should make sure to [edit] your question and make sure that it clearly describe you problem, adding some examples for expected behavior will help you define your problem/question. – Yaron Jan 22 '18 at 13:32
  • I chnged that, is it better now? – Harry Keogh Jan 22 '18 at 13:39
  • @HarryKeogh - Your question isn't clear - you might need to add some examples for what would you like to execute, and their expected behavior. – Yaron Jan 22 '18 at 13:41
1

I threw one together for you based on the comment left from @steeldriver. This is just a command line script allowing multiple input on the command line. It uses getopts for multiple options as well as adding in nohup command & for moving the scripts to the background so that the next case can run.

For further help, search the internet for bash getopts. There's so much to learn out there and you can make incredible scripts to do whatever you want.

The Script:

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

Now, if you run the script with nothing after or a -h for help, the following appears. I named my script askhelp.bsh just as an example.

~$ ./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

Then in my test script I made it so that it echoes the lines to show that they are running.

Examples:

~$ ./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

Another example:

~$ ./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

New script from request:

This script should open a new gnome-terminal for each choice made for tracking back. The commands are done for gnome-terminal. If you decide to use a different terminal, make sure you make the appropriate changes to the command in each case statement.

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

Hope this helps!

Terrance
  • 41,612
  • 7
  • 124
  • 183
  • thank you very much for your effort. When I try eg. mining.sh -1 -3 -5 I did receive confirmation "echo" that only -1 script has been launched, but neither in top command nor in new opened terminal session (it shall open automatically, right?) I dont see that extra scripts in my case -3 and -5 have started. – Harry Keogh Jan 22 '18 at 17:56
  • @HarryKeogh Oops, I forgot to drop your scripts into the background. Fixed the script. – Terrance Jan 22 '18 at 17:58
  • @HarryKeogh I don't know if you got my last message, but I added in & to the script calls to drop them to the background so that the next scripts should be able to run. I don't have your scripts, – Terrance Jan 22 '18 at 19:43
  • oh yea thats much better :) any chance, to actually recall brand new terminall session per each chosen option? so you could actually see what is particular script doing - that would be perfect! – Harry Keogh Jan 23 '18 at 06:19
  • @HarryKeogh I guess you could. You could make each section instead of being separate lines into one line that looks like this gnome-terminal -x bash -c 'echo "Starting to mine ZEC."; cd ~/Downloads/claymore.s.Zcash.v12.6/; nohup ./mine.sh & 2>&1>/dev/null';; – Terrance Jan 23 '18 at 06:35
  • probably im too stupid to find out, how to fix the proble, but upon execution I find this: /bin/mine.sh: line 49: unexpected EOF while looking for matching `"' /bin/mine.sh: line 52: syntax error: unexpected end of file btw "nohup" what is the role of this instruction? – Harry Keogh Jan 23 '18 at 19:00
  • @HarryKeogh nohup means No Hangup. I was using it so that the scripts would remain running in the background. You can probably remove that part of the section with each one opening in their own terminal. As far as your mine.sh file goes, without looking at it, I can't seel you, but it sounds like it is missing a " at the end of the line closing it on line 49. – Terrance Jan 23 '18 at 19:05
  • @HarryKeogh I removed the nohups from the line so that each terminal now will just run the script. – Terrance Jan 23 '18 at 19:07
  • @Terrence - you are the Man - muchos kudos to you :) – Harry Keogh Jan 23 '18 at 19:21
  • @HarryKeogh I am glad it helped you. If you find my answer to be the best help, make sure you select the checkmark to the left of my answer as the selected answer. Enjoy your mining! =) – Terrance Jan 23 '18 at 19:27