Hi
I have a main program script that calls another program
in another file.
After picking a number, this selected array element
isnt passed back to the main program.
1 - How do I get the echo_test script to pass back the
selected array element to the main program so that
the main program can use this element for other
purposes.
2 - In echo_test script, how do I add another condition
such that if user enters a character, it should echo
an error msg and re-prompt user to type value again ?
from below, currently it handles input with value less than
0 integer.
Advance thanks for your help
main program
#!/usr/bin/bash
##############################################################
#
# Function Name: main program of shell script
#
##############################################################
source /etc/profile
export PATH
# $PWD prints out the current directory in which
# shell script executes.
# In this case, $PWD should be /tmp/stats.
cd /tmp/stats
echo "Directory Starting point - $PWD"
echo " "
echo " "
# Determine which DB to load the stat files
echo_test.sh
# How do I pass variables from echo_test.sh to xfile main program
echo "Inside xfile: DB: ${DB_array[$input_number]}"
#############################################################
#
# End of main program of shell script.
#
#############################################################
File echo_test.sh
#!/usr/bin/bash
# Filename: echo_test.sh
# Determine which DB to load the files
echo "Which DB do you want to load with ? "
echo "Pick a number for the DB you want to load. "
echo "
1- air
2- bei
3- bp
4- choice
5- com
6- prac
7- db
8- my
9- less
10- test
11- test1
12- test2
13- test4
14- test_3_1_16
15- test_air
16- test_bei
17- test_10
18- test_20 " # end of echo statement
echo " "
echo " Enter Your DB Value: "
# Read in input value
# Keep reading input until correct integer is inputted.
# How to check for variable inputted not a number ???????
while read input_number
do
echo "Enter Your DB Value: "
echo "DB value = $input_number"
# if input_number isnt a number
if [ $input_number -lt 1 ]
then
echo "Re-Enter Your DB Value. $input_number < 1"
echo "Enter Your DB Value: "
else
break;
fi
done
echo "Your input DB value = $input_number"
# Set up DB assignments to their respective index numbers
# DB_array is a 1-D array.
# Bash supports 1-D array. Unsure if bash supports 2-D array
DB_array[1]=air
DB_array[2]=bei
DB_array[3]=bp
DB_array[4]=choice
DB_array[5]=com
DB_array[6]=prac
DB_array[7]=db
DB_array[8]=my
DB_array[9]=less
DB_array[10]=test
DB_array[11]=test1
DB_array[12]=test2
DB_array[13]=test4
DB_array[14]=test_3_1_16
DB_array[15]=test_air
DB_array[16]=test_bei
DB_array[17]=test_20
DB_array[18]=test_10
# This outputs the content of array element
echo "DB: ${DB_array[$input_number]}"
executable output
======================
bash-2.03# x*
Directory Starting point - /tmp/stats
Which DB do you want to load with ?
Pick a number for the DB you want to load.
1- air
2- be
3- bp
4- choice
5- com
6- prac
7- db
8- my
9- less
10- test
11- test1
12- test2
13- test4
14- test_3_1_16
15- test_air
16- test_bei
17- test_20
18- test_10
Enter Your DB Value:
3
Enter Your DB Value:
DB value = 3
Your input DB value = 3
DB: bp
Inside xfile: DB:
I have a main program script that calls another program
in another file.
After picking a number, this selected array element
isnt passed back to the main program.
1 - How do I get the echo_test script to pass back the
selected array element to the main program so that
the main program can use this element for other
purposes.
2 - In echo_test script, how do I add another condition
such that if user enters a character, it should echo
an error msg and re-prompt user to type value again ?
from below, currently it handles input with value less than
0 integer.
Advance thanks for your help
main program
#!/usr/bin/bash
##############################################################
#
# Function Name: main program of shell script
#
##############################################################
source /etc/profile
export PATH
# $PWD prints out the current directory in which
# shell script executes.
# In this case, $PWD should be /tmp/stats.
cd /tmp/stats
echo "Directory Starting point - $PWD"
echo " "
echo " "
# Determine which DB to load the stat files
echo_test.sh
# How do I pass variables from echo_test.sh to xfile main program
echo "Inside xfile: DB: ${DB_array[$input_number]}"
#############################################################
#
# End of main program of shell script.
#
#############################################################
File echo_test.sh
#!/usr/bin/bash
# Filename: echo_test.sh
# Determine which DB to load the files
echo "Which DB do you want to load with ? "
echo "Pick a number for the DB you want to load. "
echo "
1- air
2- bei
3- bp
4- choice
5- com
6- prac
7- db
8- my
9- less
10- test
11- test1
12- test2
13- test4
14- test_3_1_16
15- test_air
16- test_bei
17- test_10
18- test_20 " # end of echo statement
echo " "
echo " Enter Your DB Value: "
# Read in input value
# Keep reading input until correct integer is inputted.
# How to check for variable inputted not a number ???????
while read input_number
do
echo "Enter Your DB Value: "
echo "DB value = $input_number"
# if input_number isnt a number
if [ $input_number -lt 1 ]
then
echo "Re-Enter Your DB Value. $input_number < 1"
echo "Enter Your DB Value: "
else
break;
fi
done
echo "Your input DB value = $input_number"
# Set up DB assignments to their respective index numbers
# DB_array is a 1-D array.
# Bash supports 1-D array. Unsure if bash supports 2-D array
DB_array[1]=air
DB_array[2]=bei
DB_array[3]=bp
DB_array[4]=choice
DB_array[5]=com
DB_array[6]=prac
DB_array[7]=db
DB_array[8]=my
DB_array[9]=less
DB_array[10]=test
DB_array[11]=test1
DB_array[12]=test2
DB_array[13]=test4
DB_array[14]=test_3_1_16
DB_array[15]=test_air
DB_array[16]=test_bei
DB_array[17]=test_20
DB_array[18]=test_10
# This outputs the content of array element
echo "DB: ${DB_array[$input_number]}"
executable output
======================
bash-2.03# x*
Directory Starting point - /tmp/stats
Which DB do you want to load with ?
Pick a number for the DB you want to load.
1- air
2- be
3- bp
4- choice
5- com
6- prac
7- db
8- my
9- less
10- test
11- test1
12- test2
13- test4
14- test_3_1_16
15- test_air
16- test_bei
17- test_20
18- test_10
Enter Your DB Value:
3
Enter Your DB Value:
DB value = 3
Your input DB value = 3
DB: bp
Inside xfile: DB: