Passing arguments to a bash script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing arguments to a bash script
# 1  
Old 06-11-2014
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 :
Code:
sh myScript.sh abc

Inside the bash script i can use like this
myArg1=$1
wc $myArg

But here in my case, i'm using awk command inside the bash script.
So when i do similar kind of thing as mention below(the below code is not an original code):
Code:
#!/bin/bash
awk -F ","  '{  
mytable = $1;
a = CREATE TABLE $mytable(x int, y string);
}' file1.txt  file2.txt

When i run the script as:
Code:
sh testScript  xyz

It's not taking the argument $1 in the create table statement and giving the output as :
Quote:
a = CREATE TABLE $mytable(x int, y string);

The desired output is :
a = CREATE TABLE xyz(x int, y string);
So where its going wrong?
How can i pass the input parameters inside the awk command in bash script ?

Thanks in Advance,
Shree
# 2  
Old 06-11-2014
Try:
Code:
awk -F "," -v mytable="$1" '{  
....
}' file1.txt  file2.txt

The reference inside awk to the variable mytable is mytable without a dollar-sign.
# 3  
Old 06-11-2014
Code:
#!/bin/bash
awk -F "," -vmytable="${1}"  '{
a = "CREATE TABLE " mytable "(x int, y string)";
}' file1.txt  file2.txt

# 4  
Old 06-11-2014
Just refering to the title and ignoring the awk request, why not doing it like below,
as you seem to want to generate a 'simple' command?
Code:
#/bin/bash
cmd="a = CREATE TABLE $1(x int, y string);"
echo "$cmd"

# 5  
Old 06-11-2014
In addition to what Scrutinizer said, the following:
Code:
a = CREATE TABLE mytable(x int, y string);

is not a valid awk command. And, you will be executing this invalid command once for every line in both input files. To get the output you say you want, awk is an extremely expensive way to do it, but it could be done with something like:
Code:
awk -F, -v mytable="$1" '
BEGIN { printf("a = CREATE TABLE %s(x int, y string);\n", mytable)}
{       # ignore every line in both input files.}' file[12].txt

To get this output in bash (or any other shell that recognizes basic Bourne shell syntax), a much more efficient way to do it would be:
Code:
printf 'a = CREATE TABLE %s(x int, y string);\n' "$1"

Note that ShriniShoo's awk script sets the awk variable a to part of the desired output every time it reads a line from either file, but never does anything with it. That script produces no output.

Using echo instead of printf (as sea suggested) may have unwanted side effects if the user supplied argument to your script contains any backslash characters (depending on which version of echo you're using).
# 6  
Old 06-11-2014
Hi,

Thank you so much for the help. I'm able to get the desired output by doing :

Code:
#!/bin/bash
awk -F "," -vmytable="${1}" '{
a = "CREATE TABLE " mytable "(x int, y string)";
}' file1.txt file2.txt


And alos one thing :
In my awk command i have the 1st line as below
Code:
awk -F "," -vDT="$(date +%m%d%Y%H%M)" 'BEGIN {

If i want to add -v-vmytable="${1}" into the same line and alos if i want to add one more argument like -vmytable2="${2}" , how can i do it?

I tried doing :
Code:
awk -F "," -vDT="$(date +%m%d%Y%H%M)" mytable="${1}"  mytable2="${2}"  'BEGIN {

But getting some synatax related problem. How can i solve this?

Last edited by Don Cragun; 06-11-2014 at 05:54 AM.. Reason: QUOTE tags -> CODE tags again.
# 7  
Old 06-11-2014
Quote:
Originally Posted by shree11
Hi,

Thank you so much for the help. I'm able to get the desired output by doing :




And alos one thing :
In my awk command i have the 1st line as below
Code:
awk -F "," -vDT="$(date +%m%d%Y%H%M)" 'BEGIN {

If i want to add -v-vmytable="${1}" into the same line and alos if i want to add one more argument like -vmytable2="${2}" , how can i do it?

I tried doing :
Code:
awk -F "," -vDT="$(date +%m%d%Y%H%M)" mytable="${1}"  mytable2="${2}"  'BEGIN {

But getting some synatax related problem. How can i solve this?
You need -v for each variable definition:
Code:
awk -F "," -v DT="$(date +%m%d%Y%H%M)" -v mytable="${1}" -v mytable2="${2}"  'BEGIN {

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Beginner at bash scripting - need help with passing arguments

I at the moment, making a simple bash script, capable of setting up an workspace for me, so i don't have to do it manually.. Problem is though i can't seem to provide the bash script any argument, without running into my error checks, checking for input... Here is the code: #!/bin/bash... (7 Replies)
Discussion started by: kidi
7 Replies

2. Shell Programming and Scripting

Passing arguments to php script

i want to be able to pass arguments to a php script if it is being piped: cat myphpscript.php | php - $1 $2 $3 blah blah This usually works for other script languages...i.e. ruby: cat myrubyscript.rb | ruby - $1 $2 $3 blah blah so my question is, how can i pass arguments to my php... (1 Reply)
Discussion started by: SkySmart
1 Replies

3. Shell Programming and Scripting

Passing arguments to interactive program through bash script, here document

Dear Users, I have installed a standalone program to do multiple sequence alignment which takes user parameters to run the program. I have multiple sequence files and want to automate this process through a bash script. I have tried to write a small bash code but its throwing errors. Kindly... (13 Replies)
Discussion started by: biochemist
13 Replies

4. Shell Programming and Scripting

Passing arguments from a bash shell script to a command

I'm pretty new to bash scripting and I've found myself writing things like this (and the same with even more nesting): if $CATEGORIES; then if $LABEL_SLOTS; then $pyth "$wd/texify_grammar.py" "$input" "$texfile" "--label-slots" "--categories" "$CATEGORY_LIST" ... (9 Replies)
Discussion started by: burbly
9 Replies

5. Shell Programming and Scripting

passing arguments to external script

Hi! I have a python script that requires arguments and these arguments are file paths. This script works fine when executed like this: /my_python_script "file_path1" "file_path2" (i added quotes as some file names may have weird characters) the issue happens when i launch my python script... (14 Replies)
Discussion started by: gigagigosu
14 Replies

6. Shell Programming and Scripting

passing arguments to sql script

Hi Gurus, i have one requirement in unix script, i have a file called abc.txt in that few lines are there with the empid, i need to read each line and pass to .sql script. ex: abc.txt 2345 2346 1243 1234 i need to pass these arguments to .sql script rom unix ex: select * from... (1 Reply)
Discussion started by: Devendar
1 Replies

7. Shell Programming and Scripting

problem passing arguments to script

Hi, I am writing a script, which is invoked from other system using ssh. I have problems reading the arguments passing to the script. If the argument has a space in it (ex "rev 2.00"), the script considers "rev" as 1 argument and "2.00" as another. Instead i want "rev 2.00" to be considered... (5 Replies)
Discussion started by: cjjoy
5 Replies

8. UNIX for Advanced & Expert Users

Passing blank arguments to a script

All, I have a cron job script that receives several command line arguments. At some point if there are validation problems and the job cannot be run, it duplicates the entire command line into a temporary text file which is later executed as a script. Unfortunately when I pass the list of received... (7 Replies)
Discussion started by: rm-r
7 Replies

9. Shell Programming and Scripting

Passing arguments to a Perl script

I am playing around with Perl and wrote the script below that is executed from the command line, it will split data up in a file based on a value supplied. When executed you provide two arguments - the file that contains the data to be split and the character you want to split by. It works as... (4 Replies)
Discussion started by: jyoung
4 Replies

10. Shell Programming and Scripting

Passing arguments to a script

I've written a script (bgrep) for a more advanced grep command (& attached a cut down version below). I'm trying allow all grep options to be used, or in any combination. The script works fine if I type say bgrep -i -files product it will return a non-case sensitive list of matches for... (3 Replies)
Discussion started by: Kevin Pryke
3 Replies
Login or Register to Ask a Question