Backing out of a script if command doesn't return any results?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Backing out of a script if command doesn't return any results?
# 1  
Old 02-12-2010
Backing out of a script if command doesn't return any results?

Hello

I have a script which emails identifies the user ID of a user and sends them an email. A user can enter part of the name of the person he/wants to send the email to. Then I use the ypcat command to identify the UID of that person.

The problem I'm having, is building in an error trap for if the "ypcat passwd | grep -i "$RECIPIENT" | awk -F":" {'print $5 " " $1'}" command doesn't return any names (which may happen if the user makes a spelling mistake, for example).

I was under the impression that if a Unix command is executed and returns a result, then a return value of 0 is generated, hence the reason I put in if [ $! -ne 0 ] after running the ypcat command. However, this is giving me -ne: unary operator expected error message.

Has anybody any idea how I can get the script to back out if no values are returned by the ypcat command?

Many thanks
Glyn

Code:
      '3')
        TITLE="Synergy Reports - Project ${CM_PROJECT_UC}"
        clear 
        LINENUM=1  
        print_line 1 "${BOLD_ON}${TITLE}${BOLD_OFF}"
        print_line 1 "Enter the name or partial name of the report recipient (Return to Exit) > "
        read RECIPIENT
        if [ -z $RECIPIENT ]
        then
           print_line 1 "The User ID for the name you entered could not be identified."
           print_line 1
           redisplay_report_menu
        fi
        print_line 1
        ypcat passwd | grep -i "$RECIPIENT" | awk -F":" {'print $5 " " $1'}
        if [ $! -ne 0 ]
        then
           print_line 1 "The User ID for $RECIPIENT could not be identified..."
           print_line 1
           redisplay_report_menu
        fi
        INC_BY=`ypcat passwd | grep -i "$RECIPIENT" | awk -F":" {'print $1 " " $5'} | wc -l`
        LINENUM=`echo "$LINENUM + $INC_BY" -1 | bc`
        print_line 1
        print_line 1 "Enter the User ID of the recipient (Return to Exit) > "
        print_line 1
        read USERNAME
        if [ -z $USERNAME ]
        then
            print_line 1 "The User ID for $RECIPIENT could not be identified..."
            print_line 1
            redisplay_report_menu
        fi
        email_another_user_menu
        ;;

# 2  
Old 02-12-2010
Hello,

You might be referring to exit status that is stored in $? but you are using $! .

Please refer the manual once.

When the awks make presence all the greps should disappear. so you can modify the code line as ->

Code:
INC_BY=`ypcat passwd | awk -F":" '$1==$RECIPENT{print $1 " " $5} | wc -l`

Or if you only want to count the no of occurances of the match -

Code:
INC_BY=`ypcat passwd | grep -i -c  "$RECIPIENT"

As your awk liner is writing all to wc and not stdout so you can simply trim it off.

Hope this helps.

Regards,
Gaurav.

Last edited by gaurav1086; 02-12-2010 at 01:10 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Curl doesn't return csv file

Hi every body, I'm getting a trouble with my script , it's supposed to return a csv file . /usr/bin/curl -LS -D- -X GET -H "Authorization:Basic $password" -H 'Content-Type: text/csv' -L -o $OUTPUT_FILENAME $url; But I get an empty file with : {"ErrorCode":1001,"ErrorMessage":"Incorrect login... (0 Replies)
Discussion started by: beautymind
0 Replies

2. HP-UX

Find command doesn't return in shell script.

Hi All, I am using below snippet to search for a string (read from a file 'searchstring.out') in all locations (/) and then iterate over the files found to write the locations and the respective owner to an output file. However, this doesn't work as I believe the find command doesn't exit's... (11 Replies)
Discussion started by: Vipin Batra
11 Replies

3. Shell Programming and Scripting

ksh script find command not printing any results

Hello, Hitting a wall on this one. When at the command prompt it works fine: # find /home/testuser -name 'PAINT*canvasON.txt' /home/testuser/PAINT_canvasON.txt # pwd /home/testuser # ls -l PAINT*canvasON.txt -rw-r--r-- 1 root user 23 Feb 07 02:58 PAINT_canvasON.txt... (2 Replies)
Discussion started by: seekryts15
2 Replies

4. Shell Programming and Scripting

Get the return value and get the output to $results

cmd() { echo " " echo "$(whoami)@$(hostname):$(pwd)# $*" results=`eval $*` echo $results } I want to get the eval $* 's return value and pass it to a new variable $val, and get "eval $*" 's the ... (7 Replies)
Discussion started by: yanglei_fage
7 Replies

5. Shell Programming and Scripting

Script to Execute command and mail results

HI everyone, I would like to have a script to run a certain linux command and mail the results or output of this command. This shell is to be later on set as cronjob. thanks in advance. (9 Replies)
Discussion started by: patrickminas
9 Replies

6. UNIX for Dummies Questions & Answers

Why do these 2 find commands return different results?

Hi, I am using the korn shell on Solaris box. Why does the following 2 commands return different results? This command returns no results (I already used this command to create a list of files which I moved to an archive directory) find ????10??_*.dat -type f -mtime +91 However this... (15 Replies)
Discussion started by: stumpy1
15 Replies

7. UNIX for Advanced & Expert Users

Insert Data into db and return php results

This is a three step process: a) Upload date ->scrub\prep data, b) insert into db, c) return php results page. I have a question about the best practices for unix to process this. I have data from a flat file that I've scrubbed and cleaned with sed and awk. When it is complete I have an... (0 Replies)
Discussion started by: dba_frog
0 Replies

8. Solaris

uname doesn't return T5220 when it should

When I use the command uname -a I get the following SunOS SMSSC11 5.10 Generic_142909-17 sun4v sparc sun4v and not (from another system) SunOS SMSSC1 5.10 Generic_141414-07 sun4v sparc SUNW,Netra-T5220 Although this system is a T5220 as can be confirmed with prtconf snipit below System... (1 Reply)
Discussion started by: lankyG
1 Replies

9. Solaris

df command on Sol8 machine doesn't return a result

I have a sun4u system running Solaris 8. I tried running the df command but it returns a blank result. Also I'm unable to collect an explorer from this system as the OS complains that the disk is full. What could be going on here? (10 Replies)
Discussion started by: dperry1973
10 Replies

10. Shell Programming and Scripting

command line work script doesn't

Hello, I'm stuck and confused as to why when I execute things form the command line it works but when in a script it doesn't. My script: ### creating a lock on the console touch /var/run/console.lock chmod 600 /var/run/console.lock echo "$User" >>... (2 Replies)
Discussion started by: larry
2 Replies
Login or Register to Ask a Question