Get enviroment variables into AWK, but not quite that simple!


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Get enviroment variables into AWK, but not quite that simple!
# 1  
Old 04-07-2006
Get enviroment variables into AWK, but not quite that simple!

Hi,

I have a script which loops through a file and based on the fields in the file does certain things. I'm just testing the final version and come up against a problem.

One of the things is to copy files, so the line in the release.file would look like this.

COPY my_file $COPY_DIR 777

where COPY_DIR is an environment variable pointing at a directory on the server. It has to be this way because the script will be run on several machines, where $COPY_DIR is different.

At the moment the script looks as follows

cat release.file |grep -v ^# |
while read line; do
type=`echo $line | cut -d" " -f1`
case $type in
.....
.....
COPY) echo "Running COPY for `echo $line | awk '{print $2}' -`";
echo "Copying to `echo $line | awk '{print $3}' -`";
echo "Setting chmod to `echo $line | awk '{print $4}' -`";
ls -al `echo $line | awk '{print $3 "/" $2}' -`;
cp `echo $line | awk '{print $2 " " "$3"}' -`;
chmod `echo $line | awk '{print $4 " " $3 "/" $2}' -`;
ls -al `echo $line | awk '{print $3"/"$2}' -`;
;;
.....
.....
esac
done

This just doesn't work. I've looked at ENVIRON but not sure this would work, so I'm at a loss to work this out. Any help much appreciated.

Mike
# 2  
Old 04-07-2006
As always you post these things and then find the answer. My Problem was I was using awk, not nawk. Also in the file, you can't have the $, so it would be

COPY my_file COPY_DIR tmp 777

in the file and the script copy bit would be

COPY) echo "Running COPY for `echo $line | awk '{print $2}' -`";
echo "Copying to `echo $line | awk '{print $3 "/" $4}' -`";
echo "Copying to `echo $line | nawk '{print ENVIRON[$3] "/" $4}' -`";
echo "Setting chmod to `echo $line | awk '{print $5}' -`";
echo "File before copy";
ls -al `echo $line | nawk '{print ENVIRON[$3] "/" $4 "/" $2}' -`;
cp `echo $line | nawk '{print $2 " " ENVIRON[$3] "/" $4}' -`;
chmod `echo $line | nawk '{print $5 " " ENVIRON[$3] "/" $4 "/" $2}' -`;
echo "File after copy";
ls -al `echo $line | nawk '{print ENVIRON[$3] "/" $4 "/" $2}' -`;
;;
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Question about enviroment variable.

Hi Gurus, I am stuck on below issue. in my .profile. I have two variable: x=abc$123 t=xyz$ when running env command, I got below: x=abc t=xyz$ my OS is SunOS 5.10 sun4v sparc SUNW,SPARC-Enterprise-T5220 I am wondering why t=xyz$ shows exactly value? I try below: (9 Replies)
Discussion started by: ken6503
9 Replies

2. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

3. Shell Programming and Scripting

awk - Why can't value of awk variables be passed to external functions ?

I wrote a very simple script to understand how to call user-defined functions from within awk after reading this post. function my_func_local { echo "In func $1" } export -f my_func_local echo $1 | awk -F"/" '{for (k=1;k<=NF;k++) { if ($k == "a" ) { system("my_local_func $k") } else{... (19 Replies)
Discussion started by: sreyan32
19 Replies

4. Shell Programming and Scripting

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

5. Shell Programming and Scripting

Simple unix variables in script

I'm creating a script that asks a user for a variable ex read filename; read numberinput; I also have a bunch of files named file.0 file.1 ... file.55 I'm trying to delete all files (if they exist) about file.$numberinput. Can someone help me out on how to include the variable as part... (6 Replies)
Discussion started by: jenix4545
6 Replies

6. Shell Programming and Scripting

Simple Script looking for Hard Coded Variables

Hi Guys Looking for a little help with a script to grep all files looking for hard coded variables - ie its IP address... so we know which files to look at before an IP change... This is what I have - but it seems to loop and never end... Any better suggestions? #!/usr/bin/ksh #simple... (2 Replies)
Discussion started by: serm
2 Replies

7. UNIX for Dummies Questions & Answers

setting enviroment variables help

Hello everyone, I am currently trying to program in java in unix platform for the first time, so far it is OK as long as I use class libraries which come with java distribution. Unfortunately when I try to use external libraries I have to use -classpath option which I rather not doing all the... (1 Reply)
Discussion started by: run123
1 Replies

8. Solaris

Save enviroment variables

I need to save my enviroment variables,specially the $PATH.When I put it on .cshrc at next reboot I lost the configuration.How can avoid this?Thanks (2 Replies)
Discussion started by: bgf0
2 Replies

9. Shell Programming and Scripting

enviroment settings

What are the environment setting during a cron session? I have HP-UX and I want to send the output/file from a script to several e-mail addresses. I want to create an env-var to store the e-mail addresses in my .profile, but I do not know if it will be visible when a script is executed in a cron. (4 Replies)
Discussion started by: ALTRUNVRSOFLN
4 Replies

10. UNIX for Dummies Questions & Answers

Enviroment variables...

Hi guys, thanks in advance for this easy answer.... :s Ok I am trying to output the enviroment varable for host in Solaris. I have tried $HOST, $HOST_NAME, $HOSTNAME carn't find it anywhere, does someone want to put me out of my misary and tell me what it is??? :confused: :eek: Thanks... (2 Replies)
Discussion started by: B14speedfreak
2 Replies
Login or Register to Ask a Question