Checking has for a command line argument


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking has for a command line argument
# 1  
Old 05-30-2013
Checking has for a command line argument

I would like to search and print a match of the user entered $ARGV[0].
Im terrible with hashes and really dont know where to go from here.

The example data file

name location phone
Bugs holeintheground 5551212
Woody holeinthetree 6661313
Jerry holeinthewall 7771414

if the user enters "scriptname holeinthetree" at the command line, I want to return :

"Woody holeinthetree 6661313" or print an error message.

I got this far from an example in a book.


Code:
open ( IN, "peeplist" ) or die;
my @lines =<IN>;
#close (IN);
#
while ($line = <IN> ) {
  ($rtrname, $storename, $ip)=split(/ /,$line);
  $cstorename{$rtrname} = $storename;
  $cip{$rtrname} = $ip;
}
close (IN) ;

Thanks alot for the help !!
# 2  
Old 05-30-2013
Code:
#!/bin/perl
$err=1;
open (IN,"peeplist") or die;
while (<IN>) {
  if (/\b$ARGV[0]\b/) {print; $err=0;}
}
close (IN);
if ($err==1) {print STDERR "not found\n"}
exit $err;

This uses perl's $_
With $line it becomes
Code:
...
while ($line = <IN>) {
  if ($line =~ /\b$ARGV[0]\b/) {print $line; $err=0;}
}
...

\b in regular expressions is a "word boundary".

Last edited by MadeInGermany; 05-30-2013 at 04:56 PM..
This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 05-31-2013
Thanks you very much MadeInG .. that worked.

I didnt have my notebook yesterday. I looked this up last night.

why doesnt
Code:
print grep /\Q$ARGV[0]\E/, @lines

work ?


Got that written down in my notes. Wouldnt have written it down if it didnt work at some point for me.
# 4  
Old 05-31-2013
If you read the entire file into an array
you can print the array like that - without a loop.
Code:
@lines = <IN>;
print grep /\Q$ARGV[0]\E/, @lines;

Or even directly from the file
Code:
print grep /\Q$ARGV[0]\E/, <IN>;

But there is a small problem to detect the "not found" condition.
These 2 Users Gave Thanks to MadeInGermany For This Post:
# 5  
Old 05-31-2013
Ok yeah. Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add command line argument

I would like to add the ability to change the message that is displayed when timer is finished. At present it just asks for the time I want for the alarm. I think what I need is another command line argument. soundfile="/usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3"... (5 Replies)
Discussion started by: drew77
5 Replies

2. UNIX for Beginners Questions & Answers

Command line argument

Hi Guys, I'm trying to work out how to add a command line argument inside single quotes. Would anyone be able to help please as I'm going mad :) I want to be able to place the filename on command line and it then be used in a script but it needs to have quotes surrounding it. Thanks in... (4 Replies)
Discussion started by: mutley2202
4 Replies

3. Shell Programming and Scripting

Specify an entire UNIX command as a command line argument

I'm trying to write a bash script called YN that looks like the following YN "Specify a question" "doThis" "doThat" where "doThis" will be executed if the answer is "y", otherwise "doThat". For example YN "Do you want to list the file dog?" "ls -al dog" "" Here's my attempt... (3 Replies)
Discussion started by: LeoKSimon
3 Replies

4. Shell Programming and Scripting

Can a string be a command line argument?

I would like to use a string as a command line argument...is this possible using TCSH? For example say my script is called TEST and I would like to pass a string into my script stating why the test failed. EXAMPLE: TEST "Failed due to missing statement" (4 Replies)
Discussion started by: thibodc
4 Replies

5. Shell Programming and Scripting

command line argument - perl

how do i check if a command line argument is -g? for example, if command line argument equals "-g" { print "Goodbye \n"; } else { print "Welcome to the program! \n"; } (1 Reply)
Discussion started by: bshell_1214
1 Replies

6. Shell Programming and Scripting

command-line line 0: Missing yes/no argument

Hi Guys When I run the below command ssh -o 'PasswordAuthentication yes' -o 'PreferredAuthentications publickey' -i $HOME/.ssh/id_dsa Server_Name I found the below error ommand-line line 0: Missing yes/no argument Kindly help me to sort out Double post, continued... (0 Replies)
Discussion started by: Pratik4891
0 Replies

7. Programming

Command Line Argument

Hi, I have a very simple C program which will run in UNIX. When i am passing * as the command line argument, i am gettig the below output. Program: #include <stdio.h> #include "mylibrary.h" int **environ; int main(int argc,char *argv) { int i; printf("\nHello... (2 Replies)
Discussion started by: dsudipta
2 Replies

8. Shell Programming and Scripting

assign a command line argument and a unix command to awk variables

Hi , I have a piece of code ...wherein I need to assign the following ... 1) A command line argument to a variable e.g origCount=ARGV 2) A unix command to a variable e.g result=`wc -l testFile.txt` in my awk shell script When I do this : print "origCount" origCount --> I get the... (0 Replies)
Discussion started by: sweta_doshi
0 Replies

9. Shell Programming and Scripting

How to get the value in last command line argument???

Say I want to get the value of last command line argument using the value in $# (or some other way if u can suggest) how do I do it?? $"$#" `$"$#"` These don't work :( (4 Replies)
Discussion started by: amit_oddey21
4 Replies

10. Shell Programming and Scripting

passing a command line argument

I have a shell script which does the encryption of a file where i am passing the file name as a command line argument,but later on the script waits on the screen to enter Y or N what is the command i should be using on the shell script #!/bin/bash -x outfilename=file.out echo... (8 Replies)
Discussion started by: rudoraj
8 Replies
Login or Register to Ask a Question