Home > Archive > AWK > February 2005 > get shell variable
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
get shell variable
|
|
| steven 2005-02-05, 3:55 pm |
| Hi, group:
I have a script that I use awk to create a file. But I cannot get the
file. What's wrong in my code? Any suggestion will be appreciated.
#!/bin/bash
ABCD=test.tmp
awk -F'|' '{
print "cd" | " print thei ${ABCD} > ${ABCD}"
}' test.file
result is like:
thei
and there is no file created. why?
Thanks
Steven
| |
| Janis Papanagnou 2005-02-05, 3:55 pm |
| steven wrote:
> Hi, group:
>
> I have a script that I use awk to create a file. But I cannot get the
> file. What's wrong in my code? Any suggestion will be appreciated.
>
> #!/bin/bash
> ABCD=test.tmp
> awk -F'|' '{
> print "cd" | " print thei ${ABCD} > ${ABCD}"
> }' test.file
>
> result is like:
> thei
>
> and there is no file created. why?
Your code is quite twisted... What do you want to achieve?
Let awk create the file? Then write for example
print "Hello" >"literal_filename"
print "World" >awk_variable_with_filename
But your problem seems to be that you are trying to use shell
variables within awk. Pass the variable name to awk instead...
awk -v awk_variable_with_filename=test.tmp '
{ print "thei " awk_variable_with_filename >awk_variable_with_filename }'
Cannot comment on your print "cd" part, since I don't know what your
intention is.
Janis
| |
| Jürgen Kahrs 2005-02-05, 3:55 pm |
| steven wrote:
> #!/bin/bash
> ABCD=test.tmp
> awk -F'|' '{
> print "cd" | " print thei ${ABCD} > ${ABCD}"
> }' test.file
>
> result is like:
> thei
>
> and there is no file created. why?
There is no file created because there is no redirection
symbol after the print command. The > character is inside
a string. This string is treated as a "command" because of
the | character after the print command. A bit weird.
| |
| steven 2005-02-07, 8:56 pm |
| My question is how I can get the shell variable without using the awk
-v part. For the codes
#!/bin/bash
ABCD=test.tmp
awk -F'|' '{
print "cd" | " print thei ${ABCD} > ${ABCD}"
}' test.file
The ${ABCD} has no value. I tried to use ENVIRON["ABCD"] in the awk
program, like filename = ENVIRON["ABCD"], still the filename is blank.
I do not konw what cause this problem. Any suggestion?
Thanks,
Steven
Janis Papanagnou wrote:
> steven wrote:
the[color=darkred]
>
> Your code is quite twisted... What do you want to achieve?
> Let awk create the file? Then write for example
>
> print "Hello" >"literal_filename"
> print "World" >awk_variable_with_filename
>
> But your problem seems to be that you are trying to use shell
> variables within awk. Pass the variable name to awk instead...
>
> awk -v awk_variable_with_filename=test.tmp '
> { print "thei " awk_variable_with_filename
>awk_variable_with_filename }'
>
> Cannot comment on your print "cd" part, since I don't know what
your
> intention is.
>
> Janis
| |
| Ed Morton 2005-02-07, 8:56 pm |
|
steven wrote:
> My question is how I can get the shell variable without using the awk
> -v part. For the codes
>
> #!/bin/bash
> ABCD=test.tmp
> awk -F'|' '{
> print "cd" | " print thei ${ABCD} > ${ABCD}"
> }' test.file
>
>
> The ${ABCD} has no value. I tried to use ENVIRON["ABCD"] in the awk
> program, like filename = ENVIRON["ABCD"], still the filename is blank.
>
>
> I do not konw what cause this problem. Any suggestion?
To use ENVIRON, the variable has to be exported first in the shell part.
Another way to use shell variables is to assign them at the end, e.g.
awk '{print var}' var="$HOME" file
We can't tell from reading your script what it is you're really trying
to do so if you'd like more help, explain the problem you're trying to
solve.
Ed.
| |
| Bob Harris 2005-02-07, 8:56 pm |
| In article <1107790726.880829.125710@l41g2000cwc.googlegroups.com>,
"steven" <yu271637@yorku.ca> wrote:
> My question is how I can get the shell variable without using the awk
> -v part. For the codes
>
> #!/bin/bash
> ABCD=test.tmp
> awk -F'|' '{
> print "cd" | " print thei ${ABCD} > ${ABCD}"
> }' test.file
But -v var=xyz is so ! However, if your implementation of awk does
not have the -v option (hard to believe, since you have bash :-) then
you can use
ABCD=test.tmp
awk -F'|' '{
print "cd" | " print thei filename > filename"
}' filename=$ABCD test.file
Although I am unsure of what will happen in the piped print command. So
I might create the piped print command as a variable
ABCD=test.tmp
awk -F'|' 'BEGIN{ cmd=" print thei " filename " > " filename }
{
print "cd" | cmd
}' filename=$ABCD test.file
But now, I'm left with the question of what piping the text string "cd"
into a shell print command is going to do. Also on my system when I
pipe anything into a shell print command I get an error, because my awk
runs sh and not bash as the shell, and sh does not have a print command.
It does have an echo command however.
Of course if you really Really REALLY want to do shell substitution
inside of an awk script then you can not use '...' as that inhibits all
shell substitution
#!/bin/bash
ABCD=test.tmp
awk -F'|' "{
print \"cd\" | \" print thei ${ABCD} > ${ABCD}\"
}" test.file
Notice the use of \ to protect the awk " characters so that the shell
does not do the wrong thing when substituting. And this is yet another
reason to use -v var=xyz. You do not need to get into really funky
quoting to keep the shell from doing the wrong thing.
> The ${ABCD} has no value. I tried to use ENVIRON["ABCD"] in the awk
> program, like filename = ENVIRON["ABCD"], still the filename is blank.
#!/bin/bash
export ABCD=test.tmp # need to make it an environment variable
awk -F'|' '
BEGIN {
filename = ENVIRON["ABCD"]
cmd = " print thei " filename " > " filename
}
{
print "cd" | cmd
}' test.file
Still not sure what the print stuff is going to do, and the shell may
not understand print, so you may need to change that to echo
print thei ... to echo thei ...
> I do not konw what cause this problem. Any suggestion?
Complex shell substitution rules when using quoted strings, which is why
the -v abc=xyz option exists for awk.
Bob Harris
[color=darkred]
> Thanks,
> Steven
>
> Janis Papanagnou wrote:
> the
> your
|
|
|
|
|