Imitating DOS functionality: The Call Statment


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Imitating DOS functionality: The Call Statment
# 15  
Old 12-24-2007
Quote:
Originally Posted by DeepakS
Why not?

Code:
$ cat myfunc.sh 
#!/bin/sh

f1()
{
        echo Function 1
}

The () after the function name..Does that mean I can set up paramiters and pass values when I call the function?
# 16  
Old 12-24-2007
Quote:
Originally Posted by jadionne
The () after the function name..Does that mean I can set up paramiters and pass values when I call the function?
I think I found the answer to this question

Quote:
i_upper_case()
{
echo $1 | tr 'abcdefghijklmnopqrstuvwxyz' \
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
}

This is a very simple function called i_upper_case , you can probably guess what it does. The backslash at the end of the echo line is a UNIX feature that allows a command line to be continued on the next line. It tells the system to ignor the next character - in this case the newline. Note that it gets its input argument from a passed parameter ($1). So to make use of this function within a script you simply need to call it with an argument as follows:
So when I created a function as follows
Code:
######Set Path Variables
echo -==Set Path Variables==- > `cat /export/home/mldwh/rkem_refresh/logs/rkem_log_dir`$TEST
RKEM_LOG=`cat /export/home/mldwh/rkem_refresh/logs/rkem_log_dir`
RKEM_SCRIPT=`cat /export/home/mldwh/rkem_refresh/logs/rkem_script_dir`
RKEM_LOAD_FILES=`cat /export/home/mldwh/rkem_refresh/logs/rkem_load_dir`
echo -==RKEM_LOG $RKEM_LOG==- >> $RKEM_LOG$TEST
echo -==RKEM_SCRIPT $RKEM_SCRIPT==- >> $RKEM_LOG$TEST
echo -==RKEM_LOAD_FILES $RKEM_LOAD_FILES==- >> $RKEM_LOG$TEST


######Set Username and Password Variables
echo -==Set Password Variables==- >> $RKEM_LOG$TEST
IDB1USER=`cat /export/home/mldwh/maintenance/connectivity/idb1user.txt`
IDB1PWD=`cat /export/home/mldwh/maintenance/connectivity/idb1pwd.txt`
TPCUSER=`cat /export/home/mldwh/maintenance/connectivity/tpcuser.txt`
TPCPWD=`cat /export/home/mldwh/maintenance/connectivity/tpcpwd.txt`
echo -==IDB1USER $IDB1USER==- >> $RKEM_LOG$TEST
echo -==IDB1PWD $IDB1PWD==- >> $RKEM_LOG$TEST
echo -==TPCUSER $TPCUSER==- >> $RKEM_LOG$TEST
echo -==TPCPWD $TPCPWD==- >> $RKEM_LOG$TEST


RKEM_Mainframe_time ()
{
######Connect to IDB1 and capture the current time of the system
echo -==Connect to IDB1 and capture the current time of the system==- >> $RKEM_LOG$TEST
db2 "connect to idb1 user $IDB1USER using $IDB1PWD" >> $RKEM_LOG$TEST
db2 -x "select current timestamp from sysibm.sysdummy1" > $RKEM_LOG"rkem_mainframe_system_date_time.txt"
db2 -x "select current date from sysibm.sysdummy1" > $RKEM_LOG"rkem_mainframe_system_date.txt"
db2 -x "select current time from sysibm.sysdummy1" > $RKEM_LOG"rkem_mainframe_system_time.txt"
db2 Terminate
echo -==Mainframe System Date Time `cat $RKEM_LOG"rkem_mainframe_system_date_time.txt"`==- >> $RKEM_LOG$TEST
echo -==Mainframe System Date `cat $RKEM_LOG"rkem_mainframe_system_date.txt"`==- >> $RKEM_LOG$TEST
echo -==Mainframe System time `cat $RKEM_LOG"rkem_mainframe_system_time.txt"`==- >> $RKEM_LOG$TEST
}

And then do the following SH script

Code:
TEST="TESTLOG.LOG"
RKEM_Mainframe_time $TEST

The function works correctly. Is there any danger concerning this variable not getting set correctly by calling it this way?
# 17  
Old 12-24-2007
Quote:
Originally Posted by jadionne
...
The function works correctly. Is there any danger concerning this variable not getting set correctly by calling it this way?
Let's try it out Smilie

Code:
$ vi sourced.sh 
RKEM_LOG=SomeVal

RKEM_Mainframe_time ()
{
echo $RKEM_LOG$TEST
}

RKEM_Mainframe_time2 ()
{
echo $RKEM_LOG$1
}

Code:
$ cat test.sh 
#!/bin/sh

source sourced.sh

TEST=.appendval
RKEM_Mainframe_time $TEST
RKEM_Mainframe_time .123456
RKEM_Mainframe_time2 $TEST
RKEM_Mainframe_time2 .123456

Code:
$ ./test.sh 
SomeVal.appendval
SomeVal.appendval
SomeVal.appendval
SomeVal.123456

Remember the $1 from the example you found? You can refer to the function arguments using $1 - $9. When you use $TEST in your function it isn't reading a parameter - it's reading an outside variable definition. By not using $1 you actually lose some functionality.

The other thing of some interest is the assignments you make using cat /somefile. First off, the cat is not necessary - but second, and more importantly be aware that assigning this data to a variable has a compressing effect.

Code:
$ cat in_file 
1
2
3
4
5
6
7890

Code:
$ cat myscript.sh 
#!/bin/sh

VARIABLE=$(</tmp/deepak/in_file)

VARIABLE2=$(cat /tmp/deepak/in_file)

echo $VARIABLE
echo $VARIABLE2

Code:
$ ./myscript.sh 
1 2 3 4 5 6 7890
1 2 3 4 5 6 7890

Smilie
# 18  
Old 12-24-2007
Wow Dude u just blew my mind...If what I think you said is what you actualy said WOW can i go places.

First of about using cat:
Quote:
echo -==Mainframe System Date Time `cat $RKEM_LOG"rkem_mainframe_system_date_time.txt"`==- >> $RKEM_LOG$TEST
What I am trying to do here is take the data that is in the variable $RKEM_LOG and append the "rkem_mainframe_system_date_time.txt" to create a full path. Is this where you mention my improper use of cat or was it here:
Quote:
RKEM_LOG=`cat /export/home/mldwh/rkem_refresh/logs/rkem_log_dir`
This is where I read what is in this file which is the text string that makes up the begining of the path. I am doing this so that I can move these scripts around and not have to recode. Should I be using syntax that looks like this:
Quote:
RKEM_LOG=$ (< /export/home/mldwh/rkem_refresh/logs/rkem_log_dir)
Here rkem_log_dir is actualy a file that contains the desired text. The goal again was that if i needed to change path locations I would only have to change the file and the one set of code that sets the path variables...
What does the < stand for?
Also I did not see what you ment by whey you said that CAT causes compresion. Both outputs looked the same to me...

As for the variables...
Quote:
By not using $1 you actually lose some functionality.
Does the $1 indicate the first variable?...Meaning that if I had a second variable then I would use $2?
If what I am saying is true then it should look something like this

OLD CODE
Code:
RKEM_Mainframe_time ()
{
######Connect to IDB1 and capture the current time of the system
echo -==Connect to IDB1 and capture the current time of the system==- >> $RKEM_LOG$TEST

NEW CODE
Code:
RKEM_Mainframe_time ()
{
######Connect to IDB1 and capture the current time of the system
echo -==Connect to IDB1 and capture the current time of the system==- >> $1$2

And to call this I would do something like this

Code:
RKEM_Mainframe_time '/export/home' 'Filename'

Am I just totaly out of my mind or did I actualy figure out something?
# 19  
Old 12-24-2007
Quote:
Originally Posted by jadionne
Wow Dude u just blew my mind...If what I think you said is what you actualy said WOW can i go places.

First of about using cat:

What I am trying to do here is take the data that is in the variable $RKEM_LOG and append the "rkem_mainframe_system_date_time.txt" to create a full path. Is this where you mention my improper use of cat or was it here:

This is where I read what is in this file which is the text string that makes up the begining of the path. I am doing this so that I can move these scripts around and not have to recode. Should I be using syntax that looks like this:


Here rkem_log_dir is actualy a file that contains the desired text. The goal again was that if i needed to change path locations I would only have to change the file and the one set of code that sets the path variables...
What does the < stand for?
Also I did not see what you ment by whey you said that CAT causes compresion. Both outputs looked the same to me...
If you check the manpage for cat you'll see that it's used to concatenate files. When you're making variable assignments you don't need to use cat. Just go ahead and do this:

Code:
######Set Path Variables
echo -==Set Path Variables==- > /export/home/mldwh/rkem_refresh/logs/rkem_log_dir/$TEST
RKEM_LOG=/export/home/mldwh/rkem_refresh/logs/rkem_log_dir
RKEM_SCRIPT=/export/home/mldwh/rkem_refresh/logs/rkem_script_dir
RKEM_LOAD_FILES=/export/home/mldwh/rkem_refresh/logs/rkem_load_dir
echo -==RKEM_LOG $RKEM_LOG==- >> $RKEM_LOG/$TEST
echo -==RKEM_SCRIPT $RKEM_SCRIPT==- >> $RKEM_LOG/$TEST
echo -==RKEM_LOAD_FILES $RKEM_LOAD_FILES==- >> $RKEM_LOG/$TEST


######Set Username and Password Variables
echo -==Set Password Variables==- >> $RKEM_LOG/$TEST
IDB1USER=/export/home/mldwh/maintenance/connectivity/idb1user.txt
IDB1PWD=/export/home/mldwh/maintenance/connectivity/idb1pwd.txt
TPCUSER=/export/home/mldwh/maintenance/connectivity/tpcuser.txt
TPCPWD=/export/home/mldwh/maintenance/connectivity/tpcpwd.txt

I made the comment about the compression because I mistakenly thought you were trying to get the data in the files rather than assigning the names of the files. Since we brought it up, though check out the actual file listing versus the output of the variables they were assigned to. In both instances the linebreaks were removed and the whole file ended up on one line. This could be a pitfall if you attempted to do a sed or grep on that data.

I threw an extra '/' character in the lines with '$RKEM_LOG/$TEST' under the assumption that the variables are directory name and file name.

Regarding the backticks `` or $() - you'll need to sed s#/bin/sh#/bin/bash#g my previous posts where I used the $() as I don't recall this being available in standard Bourne shell. Go with the `` for portability.

Quote:
Originally Posted by jadionne
As for the variables...

Does the $1 indicate the first variable?...Meaning that if I had a second variable then I would use $2?
If what I am saying is true then it should look something like this

OLD CODE
Code:
RKEM_Mainframe_time ()
{
######Connect to IDB1 and capture the current time of the system
echo -==Connect to IDB1 and capture the current time of the system==- >> $RKEM_LOG$TEST

NEW CODE
Code:
RKEM_Mainframe_time ()
{
######Connect to IDB1 and capture the current time of the system
echo -==Connect to IDB1 and capture the current time of the system==- >> $1$2

And to call this I would do something like this

Code:
RKEM_Mainframe_time '/export/home' 'Filename'

Am I just totaly out of my mind or did I actualy figure out something?
You got it.

The only thing is when you make the call do something like:
Code:
RKEM_Mainframe_time /export/home/ Filename

or

Code:
RKEM_Mainframe_time /export/home /Filename

or better yet combine your variables in the function like this:

Code:
>> $1/$2

# 20  
Old 12-25-2007
OK here goes one more example. Im going to try this but wanted another set of eyes on this.

I have a function that sets the path variables, lets call it THE_PATH. One of the variables is called RKEM_LOG. There is a / embeded at the end of the variable definition. I have another function called RKEM_Mainframe_time. In this function I use the variables $1 and $2. Will this work:

Code:
source /export/home/mldwh/rkem_refresh/scripts/rkem_functions.sh

THE_PATH

RKEM_Mainframe_time $RKEM_LOG log_file_name.txt

# 21  
Old 12-25-2007
Regarding the use of CAT and seting the variables, I tried the following:
Code:
RKEM_LOG=/export/home/mldwh/rkem_refresh/logs/rkem_log_dir

Instead of setting the variable = to the data that was inside the rkem_log_dir file it just set the variable = to the string that was passed in or

Code:
/export/home/mldwh/rkem_refresh/logs/rkem_log_dir

This is not necisarily what is going to be in that file
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add new line after 'continue' in if statment

Hello, im creating a csv file for email reporting on issues. my problem is that after 'continue' command in if statment in a loop the new paramter writing into the log doesnt take new line. timeout 4s zabbix_get -I $ZBX -p $PORT -s $IP -k "system.run" if ; then ... (3 Replies)
Discussion started by: batchenr
3 Replies

2. Shell Programming and Scripting

Execute sql statment in korn shell

I am fairly new to writing scripts, and have gotten a lot of help from this site in the past with many of the posts. I have a question/issue with a script I am attempting to write and have a question regarding executing an sql statement inside of a loop (do while). I have in the past written... (1 Reply)
Discussion started by: josbor01
1 Replies

3. Shell Programming and Scripting

Wildcard in a tcsh if statment

Hello everyone I was hoping someone could tell me whether I can use a wildcard inside an tcsh if statement. I am trying to test the argument the user has fed the script and whether it is a .txt file. The Ides behind it is the following if (`echo $1` != *.txt) then echo "wrong... (6 Replies)
Discussion started by: smarones
6 Replies

4. Shell Programming and Scripting

syntax error for if statment test expression

Hi what's the correct way of writing if 1)if "$time_diff" -gt 5 then echo "killing hung process \n" fi 2)if test $time_diff -gt 5 then echo "killing hung process \n" fi where -time_diff=$(($Sam - $current_min)) and current_min=`date +%M` infact both are giving Syntax... (1 Reply)
Discussion started by: Anteus
1 Replies

5. Shell Programming and Scripting

-F option and - V in a single awk statment

Please let me know if I can use -F option and - V in a single awk statment. I want to import some comma separated shell variables using -F option and defining some static variables inside awk using -v option. (2 Replies)
Discussion started by: kalee
2 Replies

6. UNIX for Dummies Questions & Answers

Comparing Special characters (i.e. -,\,/) in an if statment

I need to validate the special characters of a date (the characters between the year and month & month and day). The data filed is being populated by users and read into the script vi an argument. I want to ensure that the date is a '-' (dash) and not a '/' or '\' (slash). The every thing I... (3 Replies)
Discussion started by: angelap
3 Replies

7. UNIX for Dummies Questions & Answers

Clarification for egrep statment

Can someone tell me what exactaly the following command is doing - pid_cmd="/usr/ucb/ps -axww | /usr/bin/egrep '${SUNMC2OSS_PATH}/SunMC2OSS\.jar.* sunmc2oss\.SunMC2OSS\$' | /usr/bin/egrep -v egrep | /usr/bin/nawk '{print \$1}'" Is the egrep is to check "sunmc2oss.SunMC2OSS" process inside... (2 Replies)
Discussion started by: puneet1983
2 Replies

8. Shell Programming and Scripting

if statment, based on retune value

hello im working on a project for the iphone to write a termainal based program we have bsd subsystem installed so have access to most unix command i have a executable called coordinates, which get the coordinates of the iphone when this runs it returns to the terminal the text of... (1 Reply)
Discussion started by: toliver182
1 Replies

9. Shell Programming and Scripting

what is wrong with my awk statment?

I am looking to find something in the hour(in field $2) of 03:00:07 and 04:00:07 and 05:00:07 and must contain something in field 4... why doesn't below command work? I try to use grep .. but since I am running this in loop, it's best I use the awk .. can someone please advise.. I am pretty... (1 Reply)
Discussion started by: hankooknara
1 Replies

10. UNIX for Advanced & Expert Users

hwo do I print ' in an awk statment?

hi 2 all, I'm trying 2 print " ' " in an awk statement but the sign deosn't show. The only way I came with is 2 declare the ' as a variable and call that variable. I'm trying 2 do: awk '{printf("insert into ba_memo_01 values ('%s');",$1)}' and get the output: insert into ba_memo_01 values... (7 Replies)
Discussion started by: baraka
7 Replies
Login or Register to Ask a Question