Visit Our UNIX and Linux User Community


How to set mutliple words variable from command line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to set mutliple words variable from command line
# 1  
Old 11-07-2009
How to set mutliple words variable from command line

I'm writing a script (C shell) to search for a pattern in file. For example
scriptname pattern file1 file2 filenN
I use for loop to loop through arguments argv, and it does the job if all arguments are supplied. However if only one argument is supplied (in that case pattern ) it should ask to input file name or names, and then check for pattern. How I can set variable to include multiple words input from command line so I can use it in loop. Or even better is it possible to assign command line input to argv, so I don’t have to use the same loop twice only because it is different variable name (one to loop through argv if more than on argument, and second to loop through filenames variable if only one argument supplied). Below is the part of my script which is causing problem:
Code:
set pattern = $1 
if ($#argv == 1) then
    echo "Enter name of files"
    set filenames = $< #how I can set it to accept more than word?
endif

Any help will much appreciated.

Last edited by Neo; 11-07-2009 at 01:59 PM.. Reason: Added code tags.
# 2  
Old 11-07-2009
Something like that ? (bash shell, can be translated)
Code:
#!/bin/bash
if [ ${#@} = 0 ]
then
  echo "error no arguments" # or anything else
else
     PATTERN=$1
     if [ ${#@} = 1  ]
     then
          read -p "Enter name(s) of files (separated by spaces and delimited wit quotes if containing spaces)" FILES
     else
          shift
          FILES="$@"
     fi
fi
for FILE in $FILES
do
     # what you want to do with each file i.e. :
     grep $PATTERN $FILES
done

# 3  
Old 11-07-2009
It might also be appropriate to ask why you are using C shell.

If you were weaned on it and just don't want to waste all that you have learned, that's one thing, but far too often people think "Oh, "C" shell. That must be C-like, so it's better".

Generally speaking, it's not. It has very poor redirection abilities and other faults (try searching Google for "Csh Programming Considered Harmful").

If you are going to be doing shell scripting as part of your work, you absolutely want to know Bash - you won't find too many places still using csh for anything (a few very ancient Sun BSD boxes).

And (as you saw here) if you ask for scripting help, most people aren't going to give you an answer using csh - almost always it will be Bash nowadays.
# 4  
Old 11-07-2009
Thanks for response frans, but it doesn't solve my problem. I use exactly the same analogy, as you described on bash example, but in C-shell I can't initialize variable with more than one word input from command line, or I don't know how to do this.

Tony, I know C-shell is not recommended, I have read already "Csh Programming Considered Harmful". Unfortunately, they use C-shell at the university and all students have to learn C. As soon as I'm done, I'm going to switch to bash.

Last edited by patryk44; 11-07-2009 at 04:41 PM..
# 5  
Old 11-07-2009
Hi Tony, I agree that the Bourne Shell derivatives have become the lineage of shells to use. However I do not think the suggestion that almost always people are using bash is an accurate statement. Have a look at this poll and judge for yourself. I for one think that for interactive use bash has its advantages, however for scripting in my experience ksh93 is the more advanced free shell, plus it is quite a bit faster. If you require portability you will often have to restrict yourself to the posix standard anyway. Not trying to start a this shell vs. that shell thing, just wanted to make the point that there are varying opinions in this matter.
# 6  
Old 11-09-2009
Yeah, I don't disagree - and as the poll shows, you will find ksh often also.

Too bad his university is so behind the times. On the other hand, too many Linux uswers today are unaware of anything BUT bash, so maybe it's a good thing in disguise.

Previous Thread | Next Thread
Test Your Knowledge in Computers #882
Difficulty: Easy
In October 2013 National Geographic released a file titled American Blackout which dealt with an imagined large-scale cyber attack on SCADA and the US electrical grid.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print mutliple patterns in a line using sed

Hi, I am trying to print multiple patterns in a line using sed. But it is printing only the last occurance of a pattern. If the line is the the output should be Lookup Procedure|Stored proc But the output I am getting is Stored proc The code I am using is echo... (9 Replies)
Discussion started by: kedar_laveti
9 Replies

2. HP-UX

What is the use of command set -- and set - variable?

Hi, I am using hp unix i want to know the use of the following commands set -- set - variable thanks (4 Replies)
Discussion started by: gomathi
4 Replies

3. UNIX for Dummies Questions & Answers

Adding words after a set of words

Greetings. I am a UNIX newbies. I am currently facing difficulties dealing with a large data set and I would like to ask for helps. I have a input file like this: ak 1 AAM1 ak 2 AAM1 ak 3 AAM1 ak 11 AMM2 ak 12 AMM2 ak 13 AMM2 ak 14 AMM2 Is there any possibility for me to... (7 Replies)
Discussion started by: Amanda Low
7 Replies

4. Shell Programming and Scripting

alias two words command line

Hello, i would like to alias aptitude install for sudo aptitude install, is it possible, and how ? i read the man alias page, but i think i have to use something with \ or { but i don't know exactly what. (3 Replies)
Discussion started by: harlock59
3 Replies

5. UNIX for Dummies Questions & Answers

how to extend words on a command line ?

within a unix window, how do you setup your session to extend a word, by hitting the "esc" key twice. e.g. ls -la scri (esc key, esc key) thankyou (6 Replies)
Discussion started by: venhart
6 Replies

6. UNIX for Dummies Questions & Answers

overlapping words on command line

i tried resize command , but it's not working...... (4 Replies)
Discussion started by: gaurav123
4 Replies

7. UNIX for Dummies Questions & Answers

displaying mutliple fields on command line

This is probably the dumbest question you guys can get, but I'm trying, as a complete noob, to display the unix calendar for all the months without Saturday and Sunday showing. How can I remove those fields without having to type all the fields in individually such as: cal -y | awk '{print $2,... (3 Replies)
Discussion started by: Trellot
3 Replies

8. Shell Programming and Scripting

set variable command

Hi all, I want to set a variable in ksh shell (prompt) and echo the value. $ set x=5 $echo $x But it is returning null. Can any one please help. Thanks in advance (1 Reply)
Discussion started by: ammu
1 Replies

9. UNIX for Dummies Questions & Answers

Identify duplicate words in a line using command

Hi, Let me explain the problem clearly: Let the entries in my file be: lion,tiger,bear apple,mango,orange,apple,grape unix,windows,solaris,windows,linux red,blue,green,yellow orange,maroon,pink,violet,orange,pink Can we detect the lines in which one of the words(separated by field... (8 Replies)
Discussion started by: srinivasan_85
8 Replies

10. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies

Featured Tech Videos