simple unix script help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting simple unix script help
# 1  
Old 07-22-2007
simple unix script help

when i run this script:

# Author: xtos

if [ "$#" -ne 1 ]
then
echo "Please enter ONLY one name to search, (example "$0" NAME)"
exit
fi

INPUT="grep $1 $HOME/work/phone_book"

if [ $INPUT !="$1" ]

then

echo "Listing not found, please try again."

else


NAME=" `$INPUT | cut -f1 -d":"` "
PHONE=" `$INPUT | cut -f2 -d":"` "


echo $NAME
echo $PHONE

fi




i get this when the argument is right:

$ phone work
./phone: line 11: [: too many arguments
work
(312) 675-3321

why do i get too many arguments??

and when the argument is wrong i am supposed to get this :

"Listing not found, please try again."


but i dont, and instead i get this :


$ phone wooork
./phone: line 11: [: too many arguments

line 11 is :

if [ $INPUT !="$1" ]



what am i doing wrong?? any help would be much appreciated

thanks
# 2  
Old 07-22-2007
Xtos,
If the '$1' value is not found in the file '$HOME/work/phone_book', the
result is empty:
INPUT="grep $1 $HOME/work/phone_book"

Then when you issue the 'if' statement:
if [ $INPUT !="$1" ]

The shell converts the content of 'INPUT' and the 'if' becomes:
if [ !="$1" ]

Thus, giving you the error.

If you want to know if a string is present, here is one way:
Code:
mCnt=`grep -c $1 $HOME/work/phone_book`
if [ $mCnt -eq 0 ]; then
  echo "Listing not found, please try again."
fi


Last edited by Shell_Life; 07-23-2007 at 10:44 AM..
# 3  
Old 07-23-2007
Another way

grep returns 0 if the pattern is found and 1 if not.

Code:
#!/bin/ksh
if [ "$#" -ne 1 ]
then
        echo "Please enter ONLY one name to search, (example "$0" NAME)"
        exit
fi

grep $1 $HOME/work/phone_book > /dev/null

if [ $? -eq 1 ]
then
        echo "Listing not found, please try again."
else
        NAME=" `grep $1 $HOME/work/phone_book| cut -f1 -d":"` "
        PHONE=" `grep $1 $HOME/work/phone_book| cut -f2 -d":"` "
        echo $NAME
        echo $PHONE
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help on simple script as i dont know numch about UNIX scripting

Hello All, My name is vasu and I am very new to Unix scripting, i know basic commands, but now i need to write the following script, i have tried but no luck My requirment is i am getting one our from another command as following Used:1.8TB Advisory Quota:1.8TB aaa1 Used:4.5TB Advisory... (1 Reply)
Discussion started by: VasuKukkapalli
1 Replies

2. Shell Programming and Scripting

Help/How-to - simple UNIX script / gzip (beginner)

Hey all, I would like to ask your help and sorry in advance for my ignorance since I am a complete beginner in this. I need to create a UNIX script that will: - scan a small number of folders and subfolders (see a similar file tree in the attachment) - - for each final folder (each of... (8 Replies)
Discussion started by: McNulty
8 Replies

3. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

4. Shell Programming and Scripting

need a script that does a simple task on multiple unix servers.

hi guys, i need a script that does a simple task on multiple aix servers. if possible with both telnet and ssh. the simple task i wanna do is connect to a server and run "ifconfig -a" and get the output. nextweek i need to do similar jobs on like 50 servers... :( can anybody help me with making... (2 Replies)
Discussion started by: curtis911
2 Replies

5. Shell Programming and Scripting

Cannot execute Unix command in a simple perl script

Am trying to lean perl scripting in Unix OS to automate my tasks. Please find the below perl script i have tried #!/usr/bin/perl -w print "Please Enter the VG name to be checked:"; $A = <>; print "Please Enter the free size to be checked in GB:"; $B = <>; $vgcheck = `vgdisplay... (7 Replies)
Discussion started by: jayachandran87
7 Replies

6. Shell Programming and Scripting

Simple unix variables in script

I'm creating a script that asks a user for a variable ex read filename; read numberinput; I also have a bunch of files named file.0 file.1 ... file.55 I'm trying to delete all files (if they exist) about file.$numberinput. Can someone help me out on how to include the variable as part... (6 Replies)
Discussion started by: jenix4545
6 Replies

7. Shell Programming and Scripting

Simple Unix Automation

Hi, i'm a newbie with unix and shell scripting. I'm just trying to do a script to simply automate a unix task. This are the steps on what i want to just run on a simple shell script 1. go to a specific path (cd /folder1/folder2/) 2. edit and input a number on a file (file_id) then save exit... (6 Replies)
Discussion started by: soultransit
6 Replies

8. Shell Programming and Scripting

A simple query on unix shell script

I want to write a script to go to particular path in file and run shell script from there. what will be shell script for the same. (2 Replies)
Discussion started by: shekhar_ssm
2 Replies

9. UNIX for Dummies Questions & Answers

I have a simple unix question

Hello all, I need to write a little unix script to do some work on multiple files. I need to enter the filenames from the command line (scriptname <filename> <filename>. I have written the code to do double spacing, and some line numbering but it does not take multiple files from the command... (1 Reply)
Discussion started by: smn2003
1 Replies

10. UNIX for Dummies Questions & Answers

Simple UNIX Shell Script help, PLEASE

I don't know anything about UNIX. I have been developing on NT platform and now a batch file I was running in my java code must work on UNIX instead. How do I change the below .bat file into a shell script that can be run on UNIX? Thanks in advance for your help. @ECHO OFF D:... (1 Reply)
Discussion started by: ci2a020
1 Replies
Login or Register to Ask a Question