How to make 2 separate arguments in 1 bash script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to make 2 separate arguments in 1 bash script?
# 1  
Old 08-31-2010
How to make 2 separate arguments in 1 bash script?

This is what I have:

Code:
#!/bin/bash
#ascript.sh

WORD1=`tail -n +$1 /home/gscn/word1.txt | head -1`
sed -e "s/WORD1/$WORD1/g" < /home/gscn/configtmp > /home/gscn/config

WORD2=`tail -n +$1 /home/gscn/word2.txt | head -1`
sed -e "s/WORD2/$WORD2/g" < /home/gscn/config2tmp > /home/gscn/config2

tscript.sh

When I do:
Code:
$ ./ascript.sh 1

It'll take the first word of both word1.txt and word2.txt files and put them in their respective config files. How do I make them separate? Like I want it so that I can do:
Code:
$ ./ascript.sh 1 5

Which will take the first word of the first word1.txt and put it in the respective config file, then take the fifth word of word2.txt and put it in the respective other config file. How can I achieve this?
# 2  
Old 08-31-2010
The first parameter passed to the script it $1. The second is $2, etc. Is that what you are asking?

Mike
# 3  
Old 08-31-2010
Okay, I tried this:
Code:
#!/bin/bash
#ascript.sh

WORD1=$1
WORD2=$2
echo "arg1 -> $arg1"
echo "arg2 -> $arg2"

WORD1=`tail -n +$1 /home/gscn/word1.txt | head -1`
sed -e "s/WORD1/$WORD1/g" < /home/gscn/configtmp > /home/gscn/config

WORD2=`tail -n +$1 /home/gscn/word2.txt | head -1`
sed -e "s/WORD2/$WORD2/g" < /home/gscn/config2tmp > /home/gscn/config2

tscript.sh

But when I do './ascript 1 5' it still uses the 1st word of both .txt files for each respective config instead of the 5th one for the second config
# 4  
Old 08-31-2010
Is this what you are trying to do?

Code:
WORD1=`tail -n +$1 /home/gscn/word1.txt | head -1`
sed -e "s/WORD1/$WORD1/g" < /home/gscn/configtmp > /home/gscn/config

WORD2=`tail -n +$2 /home/gscn/word2.txt | head -1`
sed -e "s/WORD2/$WORD2/g" < /home/gscn/config2tmp > /home/gscn/config2

Mike
# 5  
Old 08-31-2010
nvm I got it...I forgot $1 was already being used so I just changed the WORD2's argument to $2
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing arguments to a bash script

Hi, I wanted to pass an argument to a bash script. So that the argument is used inside the awk command inside the bash script. I know the noraml way of passing argument to a bash script as below : sh myScript.sh abc Inside the bash script i can use like this myArg1=$1 wc $myArg But... (8 Replies)
Discussion started by: shree11
8 Replies

2. Shell Programming and Scripting

Pass arguments to bash script

myscript.sh #!/bin/bash ARGA=$1 if ; then echo "${ARGA}:Confirmed" else echo "${ARGA}:Unconfirmed" fi when I run the above script from the command line, i run it as: ./myscript.sh jsmith now some times, i need to runn it this way: (8 Replies)
Discussion started by: SkySmart
8 Replies

3. Shell Programming and Scripting

Bash script with arguments

Could someone help me with the script below? I am trying to make a script having just one arguement as a command and then it executes the appropriate code #!/bin/bash if then echo "Available commands:" echo "./exec.sh cmd1" echo "./exec.sh cmd2" elif then cmd1 =... (1 Reply)
Discussion started by: spiridakos
1 Replies

4. Shell Programming and Scripting

How to make shell script arguments optional?

Here is my script: #!/bin/ksh usage () { echo " Usage: $0 <opt1> <opt2> <opt3> <opt4>" } if ; then usage exit; fi prog -a $1 -b $2 -c $3 -d $4 2>&1 | tee -a ~/$1.log I want argument 4 to be optional, so if there's no argument for opt4, that it doesn't... (8 Replies)
Discussion started by: guitarscn
8 Replies

5. Shell Programming and Scripting

How to make bash wrapper for java/groovy program with variable length arguments lists?

The following bash script does not work because the java/groovy code always thinks there are four arguments even if there are only 1 or 2. As you can see from my hideous backslashes, I am using cygwin bash on windows. export... (1 Reply)
Discussion started by: siegfried
1 Replies

6. Homework & Coursework Questions

I need to make a script that has 4 command line arguments to function properly.

I have no idea what the following means. The teacher is too advanced for me to understand fully. We literally went from running a few commands over the last few months to starting shell scripting. I am not a programmer, I am more hardware oriented. I wish I knew what this question was asking... (3 Replies)
Discussion started by: Wookard
3 Replies

7. Shell Programming and Scripting

Make a list in bash out of arguments

Hello, I have a very stupid/simple problem, but for some reason I cannot figure out...and I need your help! I am writting a bash scrip that should be executed using "my_script X Y Z T" where X Y Z and T can be any string, but there can be any number of arguments. I want my script to do... (4 Replies)
Discussion started by: jolecanard
4 Replies

8. Shell Programming and Scripting

Bash script to read a hostname and separate into variables

Hi All, I'm trying to concoct a bash script to use with a Puppet Implementation that will accept a hostname and break it down into variables. For example, my hostnames look like this --> machinename-group-building.example.com I'm looking for a way in the script to read until the first... (4 Replies)
Discussion started by: glarizza
4 Replies

9. Shell Programming and Scripting

how to make your bash script run on a machine with csh and bash

hi, i have a script that runs on bash and would like to run it on a machine that has csh and bash. the default setting on that machine is csh. i dont want to change my code to run it with a csh shell. is there any way i can run the script (written in bash) on this machine? in other words is there... (3 Replies)
Discussion started by: npatwardhan
3 Replies

10. Shell Programming and Scripting

Retain quotes from bash script arguments

Hi there, I've been scouring these forums and have found similar threads, but none apparently helped me solved my problem :rolleyes: I'd like to run a command within a bash script, but that command is provided by the user to the script and may contain quotes, which is where the problem lies.... (9 Replies)
Discussion started by: cypression
9 Replies
Login or Register to Ask a Question