Handling null input...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Handling null input...
# 1  
Old 04-10-2006
Handling null input...

Ok, so when a user inputs nothing, simply pressing enter when prompted for a phone number, I get a "./addrbkfct.sh: test: argument expected" error message. I have the following function:

Code:
addNumber(){
        echo "Phone number: \c";
        read number;
        echo $number;
        if [ -z $number ] ; then
                echo "WARNING: no number";
                addEmail "$1" "$2" "$number";
        else
                echo $number | egrep '[+]?[-]?[0-9]?[0-9]?[0-9]?[-]?[0-9][0-9][0-9][-]?[0-9][0-9][0-9][0-9]';
                if [ $? -eq 0 ] ; then
                        addEmail "$1" "$2" "$number";
                else
                        addNumber "$1" "$2";
                fi
        fi
}

# 2  
Old 04-10-2006
Got it.

Code:
addNumber(){
        echo "Phone number: \c";
        read number;
        echo $number;
        if [ -z "$number" ] ; then       # <==== should be double quoted...
                echo "WARNING: no number";
                addEmail "$1" "$2" "$number";
        else
                echo $number | egrep '[+]?[-]?[0-9]?[0-9]?[0-9]?[-]?[0-9][0-9][0-9][-]?[0-9][0-9][0-9][0-9]';
                if [ $? -eq 0 ] ; then
                        addEmail "$1" "$2" "$number";
                else
                        addNumber "$1" "$2";
                fi
        fi
}

# 3  
Old 04-11-2006
Or...
Code:
if [[ -z $number ]] ; then       # <==== use double square-brackets

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Input handling and formatting input to shell

i want to get input and depending on it create new commands for input to expect. But problem is that after giving date or month as 01-09 it is interpretation as 1-9 echo -n "ENTER DATE " read d1 echo -n "ENTER MONTH " read m1 echo -n "ENTER YEAR" read y1 o=1 i=1 d2=`expr $d1... (1 Reply)
Discussion started by: sagar_1986
1 Replies

2. Shell Programming and Scripting

Handling null Integer/string variables

I kind of found out the hard way that I am not able to manipulate the null value, the long silence that happens when there is no value returned. I am looking for PIDs, and when there is no PID return, I wanted to handle this special scenario. Here is my script. #!/bin/bash LAN_VARIABLE=... (7 Replies)
Discussion started by: lan123
7 Replies

3. Shell Programming and Scripting

Insert string 'NULL' where there is a null value

I have an input file having 7 fields delimited by , eg : 1,ABC,hg,1,2,34,3 2,hj,YU,2,3,4, 3,JU,kl,4,5,7, 4,JK,KJ,3,56,4,5 The seventh field here in some lines is empty, whereas the other lines there is a value. How do I insert string NULL at this location (7th loc) for these lines where... (8 Replies)
Discussion started by: zilch
8 Replies

4. Shell Programming and Scripting

Null Handling in Until loop. . .loop won't stop

Hi Im running this script, which is supposed to find the max value build some tables and then stop running once all the tables are built. Thing is , it keeps assigning a null value to $h and then $g is null so it keep building tables i.e. testupdateNUL. How can I stop this? Here is what I have: ... (4 Replies)
Discussion started by: brandono66
4 Replies

5. Shell Programming and Scripting

replacing spaces with null or 0 in the input file

hi i have records in my input file like this aaa|1234||2bc||rahul|tamilnadu bba|2234||b4c||bajaj|tamilnadu what i am expecting is in between two pipes if there is no character it should be replaced with null or 0 so my file will look like this aaa|1234|null|2bc|0|rahul|tamilnadu... (4 Replies)
Discussion started by: trichyselva
4 Replies

6. UNIX for Dummies Questions & Answers

Handling input from a ls in a script

I'm trying to write a script that will handle the input from the ls command in my script so I can then manipulate the data. For example, I want to capture the output of the ls command in my script and then do a differences between the filename received in to another directory. ls |... (1 Reply)
Discussion started by: spookyrtd99
1 Replies

7. UNIX and Linux Applications

handling maximum number characters in an input file

Hi, Can anyone help me? An input file has three lines. Each line should only be 2098 as number of characters however line 2 of the input file has more than the maximum number of characters, it exceeded up to 4098. What should I do so that could handle maximum number of characters? that it could... (1 Reply)
Discussion started by: chrysSty
1 Replies

8. Shell Programming and Scripting

handling null values in files

Hi , I have a script where i will remove header and trailer record and ftp to another server. I'm using the code: latestfilename=`ls filename_* | tail -1` echo "Latest filename = $latestfilename" sed '1d;$d' $latestfilename > a.ftpedfile I have an issue if input data is having null... (1 Reply)
Discussion started by: ammu
1 Replies

9. Shell Programming and Scripting

Null Character Handling

Hi All, I have a problem with Null values while reading line by line from a text file. I wrote a shell script to read set of file names from a text file line by line, and zipping the each individual file and copying those zip files into some separate directory, and removing the original file... (3 Replies)
Discussion started by: npk2210
3 Replies

10. Shell Programming and Scripting

Null handling in scripts

Hi, I face some problem with handling of nulls. I declare a variable - say i - and intialise to 0. Later I read it from console, wherein if I dont give any variable and press return key, I get this error: "0403-004 Specify a parameter with this command" Is there anyway to handle this error? ... (3 Replies)
Discussion started by: mohanprabu
3 Replies
Login or Register to Ask a Question