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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assigning Unix Function Return value???

Status
Not open for further replies.

itpmguy

Programmer
Apr 25, 2002
31
US
I have written sub routines in unix. How can we get the return value of UNix sub routines. The return function gives the exit status only.

here's the command
position=`expr substr $longline 1 5`

can i write a function as follows and assign the value
get_pos () {

string=$1
$pos1=$2
pos2=$3

expr substr $longline $pos1 pos2

}

This should be called as:

firstname=get_pos $line 0 7
lastname=get pos $line 8 10

is it possible to do this in UNIX?

ANy suggestions are greatly appreciated.

Thanks



Cheap Computer Part Supply Buying Tips
VOIP Phone Service Guide
 
You have to use command substitution:

if ksh:

firstname=$(get_pos $line 0 7)
lastname=$(get pos $line 8 10)

if bourne sh:

firstname=`get_pos $line 0 7`
lastname=`get pos $line 8 10`



HTH,

p5wizard
 
Thanks. Actually, I was using firstname=`get_pos $line 0 7`
in korn shell and I was getting error :)

The firstname=$(get_pos $line 0 7) worked good in Korn shell.

Here's a question about reading files. I am reading a text file well in unix and getting all the fields based on length specified. The end of the line has blanks if there is NO real value for that field. In such cases, The programs aborts.

For ex. if the last part of the line (positions 100 to 102) of the file shows "state" code then to read that I will be assigning as follows:

state=$(get_pos $line 100 2)

but if the position 100 to 102 is blank, the program exits. How can I make the program read the blanks as values and move on to the next line??

Thanks!

Cheap Computer Part Supply Buying Tips
VOIP Phone Service Guide
 
The blanks will be in the variable, but it depends on what your script does with the variable and if the shell can interpret to value (shell must be able distinguish between the blanks in the var and other whitespace).

if [ $var = ' ' ]

would give an error if the var has two blanks, but

if [ "$var" = ' ' ]

gives the shell a chance to identify which blanks are which.



HTH,

p5wizard
 
Thanks p5wizard.

I have not tried this yet but I wanted to inform you briefly about what the program does.

This program assigns the values to variables from the input text file and then loads it into a database table. The problem I was having is that in the very first line of the file, positions 100 - 102 (which is the end of the file) are blanks and the program exits without going to the second line.

The program probably thinks that there is NO position 100 - 102 and that's why it gives an error (the error code is 1) and exits. I will try your solution on Monday and follow up.

Some of them have suggested Perl for this but since this is very simple text file handling program, I wanted to put all the functionality into Korn shell program.

Thanks


Cheap Computer Part Supply Buying Tips
VOIP Phone Service Guide
 
On what condition does the script exit? Can you post some more of the logic?

Have you tried:

[tt]state="$(get_pos $line 100 2)"[/tt]

Annihilannic.
 
if the line is too short,

expr substr $line 100 2

would result in a null string

I would also code it a bit differently, to allow for embedded whitespace in $line

expr substr "$line" 100 2

or

expr substr "${line}" 100 2

but that last version is just personal preference.



HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top