Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi I have a main program script

Status
Not open for further replies.

chz013

Programmer
Jan 5, 2003
73
US
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 &quot;Re-Enter Your DB Value. $input_number < 1&quot;
echo &quot;Enter Your DB Value: &quot;
else
break;
fi
done
echo &quot;Your input DB value = $input_number&quot;


# 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 &quot;DB: ${DB_array[$input_number]}&quot;




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:

 
They're both separate programs - variables updated in one of them are not reflected back to the other

You could try this
Code:
# Determine which DB to load the stat files
source echo_test.sh

The source command runs a program as part of the current program, not as a separate program.
See the source /etc/profile at the start of the script.
 
Salem
thanks I took your approach it works. that answers 1.
What about 2?

3 - I want to copy files into a directory.
But this directory might or might not exist before.
If it doesnt exist, I have to create one so that files
can be copied into it. The name of the directory is
dependent on the input I gave in the DB number I pick
in echo_test script.

Inside my copy_file() function of my main program, I have

copy_file() {

echo &quot;Copy files...&quot;


# while reading file and not enf of file
# tar_filename is a record in /tmp/stats/test
while read tar_filename; do

echo $PWD/$tar_filename
echo &quot;Inside copy_file function. &quot;
echo &quot;${DB_array[$input_number]}&quot; #output is correct but directory for it doesnt exist before.

# If this database file exists then copy it to the specified path
# else create this directory in the path and then copy it to the specified path
if [ -ed &quot;/data/${DB_array[$input_number]}&quot; ]
then
cp -r $tar_filename /data/${DB_array[$input_number]}
else
mkdir -m 777 /data/${DB_array[$input_number]}
cp -r $tar_filename /data/${DB_array[$input_number]}
fi


# done with reading file
done

}


executable output
=================

Copy files...
/tmp/stats/20030623
Inside copy_file function.
test_bei
./xfile_v5.sh: [: -ed: unary operator expected


why is that ?
Is my if condition checks wrong ?
 
> if [ -ed &quot;/data/${DB_array[$input_number]}&quot; ]
I'm not aware of any double letter operations for the test command.
-e tests a files existance
-d tests a files existance, and whether its a directory.
 
this function checks if $1 is an integer

Not sure if it works in bash

isnum()
{
case ${1} in
+([0-9])) return 0 ;;
*) return 1 ;;
esac
}

or in your case where you're testing for an integer > 0

let $x 2> /dev/null

and check the return code

Salem's right about [ ed ...

And you probably need to test if the dir exists and is writable by you.

if [ -d $dir -a -w $dir ]


In bash, can you load the array this way?

DB_array=(junk air bei ...) (junk loads element 0)



 
Thanks Salem, pmurray99 for your responses.

4- How does one type IO command at the prompt
such that, when main program script is run,
one could see the prompt like below:
=====================================================
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:
=========================================================

And at the same time prints all the echo statements
in a file.

I tried
xfile.sh > /tmp/output
and
xfile.sh &> /tmp/output

None of these work in that nothing is printed on screen
of the prompt to allow me to pick a DB from the list.

Thanks for your help
 
try

xfile.sh | tee -a mylog

But I'm not sure I understand what you're asking


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top