How to substitute one argument into another?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to substitute one argument into another?
# 1  
Old 04-08-2012
How to substitute one argument into another?

Hello Friends,
[In bash] I am not able to solve something which looks silly..after spending some time around it, thought of positing it here.
I want to get $2, $3... into user. but I want to control the sequence by count.
I want to assign $2 user..then $3 to user..then $4 to user etc.
how can I substitute that with count

Code:
count=2;
while loop
{
user=$[$count]
echo "$user"
count=$(($count+1))

}

Moderator's Comments:
Mod Comment Welcome to the UNIX and Linux Forums. Please use code tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-08-2012 at 04:34 AM.. Reason: code tags
# 2  
Old 04-08-2012
try this,

Code:
 
count=2
eval user=\${${count}}
echo "$user"

# 3  
Old 04-08-2012
No need for the curly brackets.. Try:
Code:
count=2
to=6
while [ $count -le $to ]
do
  eval user=\${$count}
  echo "$user"
  count=$((count+1))
done

# 4  
Old 04-08-2012
Code:
[root@node2 ~]# cat get 
#!/bin/bash

set -- {a..c}
for((i=1; i<=${#}; ++i)); do
     echo ${!i}
done
[root@node2 ~]# bash get 
a
b
c

# 5  
Old 04-08-2012
And if really coding in bash code you also do this:
Code:
for count in {2..6}; do
  user=${!count}
  echo "$user"
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

2. UNIX for Dummies Questions & Answers

Substitute in VI

Hi there, i am updating a file on UNIX and have many lines as per below : listen:x:37:4:Network Admin:/usr/net/nls: i would like to substitute from the :/usr to the end of the line. so at the moment im using this : :s/"\/$/ /g but i get an error.can anyone help? thank you (3 Replies)
Discussion started by: brian112
3 Replies

3. Shell Programming and Scripting

Make script that run with argument if not run from configuration file argument

Hello, Is there any method thorugh which script can take argument if pass otherwise if argument doesn't pass then it takes the argument from the configuration file i.e I am workiing on a script which will run through crontab and the script will chekout the code ,zip and copy to the... (3 Replies)
Discussion started by: rohit22hamirpur
3 Replies

4. UNIX for Dummies Questions & Answers

substitute (')

I usually use : Code: awk '{gsub(/xxx/,"yyy");print}' to substitute xxx with yyy. I have a problem substitute an expression like Code: x ' y Because of the ( ' ) Any idea on how to get over this problem? Thanks (2 Replies)
Discussion started by: cosmologist
2 Replies

5. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

6. Shell Programming and Scripting

vi substitute

My question is how would I substitute for ceratain number of occurences in a line? If this is my input rjohns BFSTDBS01 Standard Silver NPRO30DINCR 2 Client Is it possible to change the first 3 occurences of space " " to a comma? (7 Replies)
Discussion started by: reggiej
7 Replies

7. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

8. Shell Programming and Scripting

How to substitute?

Hi, I have query terms like this: a) apple bannana b) apple bannana AND chickko c) "milk shake" OR Graphes orange whereever there is space substitue with AND operator. I tried like this: (2 Replies)
Discussion started by: vanitham
2 Replies

9. UNIX for Dummies Questions & Answers

How to find the last argument in a argument line?

How to find the last argument in a argument line? (4 Replies)
Discussion started by: nehagupta2008
4 Replies

10. UNIX for Dummies Questions & Answers

Substitute in vi

I know in vi you can do :%s/replaceme/withthis/ but if i want to find all lines say without a # at the begining and I want to put it in how would that command be formatted? I can't figure it out for the life of me. #comment blah1 hey1 grrr1 #comment #blah1 #hey1 #grrr1 (5 Replies)
Discussion started by: kingdbag
5 Replies
Login or Register to Ask a Question