Can I use $1 several times in shell program?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can I use $1 several times in shell program?
# 1  
Old 09-14-2004
Can I use $1 several times in shell program?

Hi,
I am new to Unix and shell programming.
I am trying to write a shell program to read 4 variables from command line.
For example,
Please enter your name: somebody
Please enter your address: address
plase enter your phone: phone

I'd like to save all threee variables in my program for further use.
I used

if [ -z "$1" ]
then
$UCB/echo "Please enter your name"
$UCB/echo " "
$UCB/echo -n "ENTER: "
fi
read name
$UCB/echo "Please enter your address"
$UCB/echo " "
$UCB/echo -n "ENTER: "

if [ -z "$1" ]
then
name=""
else
read name
fi

It's not working. Can someone tell me what's wrong with it?

ThanX
# 2  
Old 09-14-2004
Hmmm... You're doing something strage here.

You're grabbing the value for name if $1 is empty. Fine. But then you're
saying if $1 is empty (which it still will be), then set name="", i.e.
lose the value it was just set to?!

Anyway, yes, $1 can be referenced as many times as you want, as long as you're
not trying to reference it inside of a function.

I'd personally save $1 to a variable within the program, i.e. "first_arg=$1" or
whatever.

Cheers
ZB
# 3  
Old 09-14-2004
Data

I am sorry. It was typo.
second name variable should be address.

I tried address= $1. It didn't work.
Is it because within one function?
How do I know that if I am within same function?
# 4  
Old 09-14-2004
Well, for one, make sure you have no spaces in the assignment, e.g.
address="$1" will work - address= "$1" will not.

Also, notice that I have surrounded the $1 in quotes, this takes care of whitespace within the variable.

For completeness, I'll also say that certain use of "set" will set the value of $1 (and other positional params depending on usage), although this isn't an issue within your script.

It might be worth you describing exactly what it is that you are trying to achieve with your script.

When you say "it doesn't work", why doesn't it work? What is the expected behaviour? What is the actual behaviour exhibited? Etc. As Johnny5 would say "More information....".

From your sample script, it doesn't look like you are using functions so that shouldn't be an issue.

Cheers
ZB
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Running a program multiple times to search pattern and assign structure

Hi all, I have a big file (n.txt) with following pattern: ATOM 1 N SER A 1 122.392 152.261 138.190 1.00 0.00 N ATOM 2 CA SER A 1 122.726 151.241 139.183 1.00 0.00 C TER ENDMDL ATOM 1 N SER A 1 114.207 142.287 135.439 1.00 0.00 ... (3 Replies)
Discussion started by: bioinfo
3 Replies

2. UNIX for Dummies Questions & Answers

shell program- how many times a function is called

We have a program source C and is required to indicate how many times each function is called from the C program. also print the line number where there is a call. I've tried something like this: #!/bin/sh for i in $*;do if ! then echo $i is not a C file. else echo $i... (0 Replies)
Discussion started by: oana06
0 Replies

3. Shell Programming and Scripting

Tabbing a line a variable number of times in shell

Hi, I had another question. I was wondering if there was a way to tab a line a variable number of times in tcsh. To go into details, I want to tab a line by how deep a file is in its path. So here is an example code: set filea=/blah1/blah2/blah3 set fileb=/blah1/blah2/blah3/blah4 set... (4 Replies)
Discussion started by: chu816
4 Replies

4. Programming

Compare times to run a program - Serial vs MPI

Hi, I have a fortran program with serial and MPI version. I want to compare the time taken by these programs to run. I use ifort/gfortran compiler. How to compare the time taken by each program to run? Is there any sample code for comparison? Thanks, rpd (1 Reply)
Discussion started by: rpd25
1 Replies

5. Programming

Problem with implementing the times() function in C (struct tms times return zero/negative values)

Hello, i'm trying to implement the times() function and i'm programming in C. I'm using the "struct tms" structure which consists of the fields: The tms_utime structure member is the CPU time charged for the execution of user instructions of the calling process. The tms_stime structure... (1 Reply)
Discussion started by: g_p
1 Replies

6. UNIX for Dummies Questions & Answers

script help shell session times out

while read s1 ; do url= suf=ignore=.csv; wget $url$s1$suf; sleep 5; cat /home/joey/public_html/charts/program/fn\charts/data/tickers/header.txt > $s1.txt; chmod 777 $s1.txt; sed '1d' table.csv?s\=$s1 >> /home/joey/public_html/charts/program/charts/data/tickers/$s1.txt; rm -Rf... (0 Replies)
Discussion started by: harte
0 Replies

7. Shell Programming and Scripting

Shell script to run x times

Hi, First i need to cd to this directory $SWDIR/util Second i need to run the following either 4 times or 20 times ./swadm add_process 1 BG Y how can i put this in a script which should ask for user input on how many times you want to run this Thanks, (5 Replies)
Discussion started by: lookinginfo
5 Replies

8. Shell Programming and Scripting

[bash] Run a program many times

Hi I'm running a program many times with differents input. I mean that i run my_prog with some parameters and i wait till the end, then i start again another simulations with some others differents parameters. Is possible to make it automatic with a script bash. Maybe i need some... (2 Replies)
Discussion started by: Dedalus
2 Replies

9. UNIX for Advanced & Expert Users

Checking mem usage at specific times in a program

Hi all, I'm running a simulator and I'm noticing an slow increase in memory for long simulations such that the simulation has to end because of a lack of memory. A colleague of mine ran Valgrind memcheck and reported that nothing of interest was reported other than known mem leaks. My advisor... (2 Replies)
Discussion started by: pl4u
2 Replies

10. Shell Programming and Scripting

One shell script --2 parts need to execute during different times

Hi All, Have one job . first part of it needs to run on five days of the week , while second part of the job needs to run on two days of the week. How do I go abt this? Thanks (2 Replies)
Discussion started by: cbecentral
2 Replies
Login or Register to Ask a Question