- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: AWK environment variables
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-24-2002 06:03 AM
тАО10-24-2002 06:03 AM
I am trying to figure out how to set a variable in the middle of an awk statement i.e.
#set a variable that I need
#to reference within an awk
#statement (called FIELD_ERR)
FIELD_ERR=0
for
do
while read x
do
x=$(echo $x | awk -F"|" '{
if (NF == 20) {printf
#try and change the variable here
else if (NF > 20) {ENVIRON ["FIELD_ERR"] = "1"}
else {print $0} }')
echo $x >> ${db02file}.out
done < $db02file
# but this is where I can't reference it!
#if I print it, it is still 0 despite the
#condition above being met
if [ $FIELD_ERR -eq 1 ]
then
echo "reached"
fi
done
echo $FIELD_ERR
any help would be appreciated with regards to referencing variables declared outside an awk statement, that are then used inside it!
let me know if this is not clear!
cheers
John
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-24-2002 06:08 AM
тАО10-24-2002 06:08 AM
SolutionI think that you can't do it this way ... when you use a awk command, it forks a new process, so you will never be able to access any env var in this environment from its parent process. ENVIRON can only be used to READ variables from env.
Regards,
Jean-Louis.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-24-2002 06:12 AM
тАО10-24-2002 06:12 AM
Re: AWK environment variables
have a child change the environment of a parent process. That can't be done - the direction of data flow is one way - parent to child. About the only way to do something like this is to have the child process write to a file (or a named pipe) and then the parent could read the file (or pipe) and set the variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-24-2002 06:27 AM
тАО10-24-2002 06:27 AM
Re: AWK environment variables
Perhaps try :
ERRFILE=/tmp/awk$$
FIELD_ERR=0
for
do
while read x
do
x=$(echo $x | awk -v errfile="$ERRFILE" -F"|" '{
if (NF == 20) {printf
#try and change the variable here
else if (NF > 20) {system("touch " errfile) }
else {print $0} }')
echo $x >> ${db02file}.out
done < $db02file
if [ -r "$ERRFILE" ]
then
echo "reached"
fi
done
Regards,
Jean-Louis.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-24-2002 07:20 AM
тАО10-24-2002 07:20 AM
Re: AWK environment variables
You could accomplish this by doing something like:
Add: BEGIN{exitcode=0} to the awk code
Change:
else if (NF > 20) {ENVIRON ["FIELD_ERR"]="1"}
to
else if (NF > 20) {exitcode=1}
Add: END{exit exitcode} to the end of the awk script.
Then in the shell script portion right after the awk call:
export FIELD_ERR=$?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО10-24-2002 05:33 PM
тАО10-24-2002 05:33 PM