Operating System - HP-UX
1834829 Members
2509 Online
110070 Solutions
New Discussion

Help me the shell script...

 
darkdream
Regular Advisor

Help me the shell script...

The content of a file named "number" :

2210
2234
3560

I want to write my own shell script "test.sh" which translate the number to the program using the command such as :
sh test.sh 3560

This script is used to locate the rows of the number.

So anybody can give me some suggestions to write the script, thanks!
10 REPLIES 10
Pete Randall
Outstanding Contributor

Re: Help me the shell script...

grep 3560 number


Pete

Pete
Bill McNAMARA_1
Honored Contributor

Re: Help me the shell script...

not sure what result you want?

loop to go line by line through file:
--
for i in $(cat /path/to/number)
do
#perform action
done
--

look for occurance of input arg number 1 in file:
--
grep $1 /path/to/number
echo $?
--

Later,
Bill
It works for me (tm)
Ricardo Bassoi
Regular Advisor

Re: Help me the shell script...

grep 3560 number

If you never try, never will work
Dario_1
Trusted Contributor

Re: Help me the shell script...

Hi!

As mentioned before try using the grep command.

grep 2210 /path/filename

Regards,

DR
John Meissner
Esteemed Contributor

Re: Help me the shell script...

here's a script for you -

#! /usr/bin/ksh
echo "enter the number you wish to look for"
read numb
cat number |
while read line
do
linumber=1
if [ $line = $numb ]
then
echo $linumber $numb
((linumber=$linumber+1))
elsif
((linumber=$linumber+1))
if
done
All paths lead to destiny
John Meissner
Esteemed Contributor

Re: Help me the shell script...

here's a script for you -

#! /usr/bin/ksh
echo "enter the number you wish to look for"
read numb
cat number |
while read line
do
linumber=1
if [ $line = $numb ]
then
echo $linumber $numb
((linumber=$linumber+1))
elsif
((linumber=$linumber+1))
fi
done
All paths lead to destiny
John Meissner
Esteemed Contributor

Re: Help me the shell script...

sorry - My first post had a spelling mistake.... the 2nd to last line.... "if" should be "fi" - I corrected this in my second post
All paths lead to destiny
Darren Prior
Honored Contributor

Re: Help me the shell script...

Hi,

I think the -n option to grep is what you're after; it'll print the line number of any lines found.

Check the man page for grep(1) for more info. In your script you'll want to use some error checking to cope with times when the number doesn't exist in the file.

Please don't forget to assign points to people that have helped you; it helps others determine which answers are good; and helps reward people for their time and effort helping you.

regards,

Darren.
Calm down. It's only ones and zeros...
John Meissner
Esteemed Contributor

Re: Help me the shell script...

now I feel dumb :) I should have remembered that instead of writting that script :) oh well
All paths lead to destiny
Darren Prior
Honored Contributor

Re: Help me the shell script...

John - don't forget there's a million ways to skin the Unix cat! Actually it's more common for me to post a script and see the next post contains a really quick, neat method. :)

regards,

Darren.
Calm down. It's only ones and zeros...