Bash Help

eternaloptimist

Well-Known Member
Joined
Jul 10, 2013
Messages
175
Reaction score
1
Hey all, bash noob here.

I have some scripts that work fine on their own. I now need to make them work from 1 file inside a loop and have the user choose which file to run.
./greet and ./shout work fine on their own i.e outside the loop.
I appreciate all the help I get, thanks.

Code:
#!/bin/bash
while true; do
        #how do i collect the args into an array so as to use then like $1 $2 $3 etc?
	read $@
	case "$*" in
		a)
			./greet.sh $1
			;;
		b)
			./shout  $1 $2
			;;
		*)
			echo 	"bye"
		        exit 1
	esac
done
 
Code:
#!/bin/bash
while true; do
        #how do i collect the args into an array so as to use then like $1 $2 $3 etc?
	read $@
	case "$*" in
		a)
			./greet.sh $1
			;;
		b)
			./shout  $1 $2
			;;
		*)
			echo 	"bye"
		        exit 1
	esac
done
Can you give some examples of how you'd like to call the script, and what you expect the output to be?
 
Can you give some examples of how you'd like to call the script, and what you expect the output to be?


$ ./script
#enter infinite loop
a Sammy
#returns
"Hello Sammy!"
b Sammy Jane
#returns
"Sammy, Jane says hi!"
hhhhh
#exits the loop

I have 2 files, the first one has this
1st file ./greet
echo "Hello $1!"
2nd file ./shout.sh
echo "$1, $2 says hi!"

I want to call these files using either a or b from inside the loop.
 
Why not use something like Python?

I don't see the point of a Bash script if you aren't doing something OS related...
 
You need to accept the 'a Sammy' input as a string and then parse it for the various pieces.

From a quick glance, what you have there looks basically correct:
Start loop
Read input
Process input(if given)
Go back to start loop
 
Code:
#!/usr/bin/bash
while getopts a:b:h opt
do
  case $opt in
        a) ./greet.sh $OPTARG;;
        b) ./shout.sh $OPTARG;;
        h) echo "Help!";;
  esac
done

Code:
$ ./t.sh -a Jain -b "Tim Tom"
+ getopts a:b:h opt
+ case $opt in
+ ./greet.sh Jain
Hello Jain!
+ getopts a:b:h opt
+ case $opt in
+ ./shout.sh Tim Tom
Tim, Tom says hi!
+ getopts a:b:h opt
 
Last edited:
thanks for the help everyone, really appreciated! I got it to work by adding
read -r arg1 arg 2 arg3
 
Top
Sign up to the MyBroadband newsletter
X