LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 03-04-2005, 01:57 AM   #1
Emmanuel_uk
Senior Member
 
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,604

Rep: Reputation: 47
cannot export result from awk into a variable in a bash script


Hi

I want to export the result from a awk command into a variable in a bash
script. How do I do that?
PS: I do not want to have to create a file and then read from it to
load the variable. I tried redirection, but no luck, not enough experience.

(I have read the awk manual and fiddled quite a bit.
I also looked at some existing scripts, no inspiration so far.
Will have to learn by example am afraid. thanks)

I call my script with ./myscript.sh --*.txt
(In practice I want to get a substr of the argument passed to the script)
There must be zillion way of doing it... but fow now I am trying with awk.

Code:
 
#!/bin/bash
myvar=$1
echo "myvar is $myvar"

myshortvar=""
gawk 'BEGIN { print substr("'$myvar'",3) }'
#this above works fine. Now how do I return the result to myshortvar?

#ls -l $myvar
#mydir=`pwd`
#df -hl $mydir
 
Old 03-04-2005, 04:14 AM   #2
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
Hi,

Backticks should do it...

myshortvar=`gawk 'BEGIN { print substr("'$myvar'",3) }'`

John
 
Old 03-04-2005, 05:40 AM   #3
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
It is usually better to use the $() notation, rather than backticks, because parenthesis can be used several times; For example: RPMforX=$(rpm -qf $(which X)).

So you would write something like that:
myVar=$(awk command)

But be carefull! Be it with backticks or with parenthesis, if you want to keep the original amounts of spaces, as well as line breaks, you'll have to use quotes, too:
myVar="$(awk command)"

Note that (at least using parenthesis) you can use quotes in the command. So this would work for example:
pidList="$(ps -e | grep -v "PID" | awk "{print \"${USER}: \" \$1}")"

Yves.
 
Old 03-04-2005, 06:55 AM   #4
Emmanuel_uk
Senior Member
 
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,604

Original Poster
Rep: Reputation: 47
Thanks/Merci,

I tried backquoting but maybe with too many spaces, or wrong nesting,
and it was also before I could get the syntax right for the inner variable in substr.
I let you know once I have tried your suggestions

Thanks for the help
 
Old 03-07-2005, 01:54 AM   #5
Emmanuel_uk
Senior Member
 
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,604

Original Poster
Rep: Reputation: 47
dirr a ls improved function with hard drive space left

Thanks guys, it now works

I have put this home-made newbee function in my bashrc.
Zillions did it before, I am sure, in a more elegant way

dirr ()
{
# This function is equivalent to a dir / ls
# with added at the last line the space left in the current drive / partition
#
# call the function with dirr [--arg1]
# where -- must preceed the argument (otherwise *.txt as an arugment for example will
# not work because of a global file auto-completion)
#
argu1=$1 #retrieve fist argument

#Retrieve -- and trap errors
shortargu0=`gawk 'BEGIN { print substr("'$argu1'",0,2) }'`
if [ "$shortargu0" == "--" ] || [ "$shortargu0" == "" ]; then
shortargu0="valid argument at start"
else
echo "Argument must be preceeded by -- (minus-minus)"
return 0
fi

# gawk 'BEGIN { print substr("'$argu1'",3) }'
#extract the substring that ignores the 2 first characters (--)
shortargu1=`gawk 'BEGIN { print substr("'$argu1'",3) }'`

#echo "myvar is now $shortargu1"
ls -l -F --color=auto $shortargu1 #call a long ls
mydir=`pwd` #find where we hare, more to the point we are interested in the drive / partition itself
df -hl $mydir #display space left
return 0
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to export environment variable from a bash script dimaash Linux - Newbie 20 08-09-2011 07:12 AM
export call in bash script Donald1000 Linux - Software 10 03-12-2009 08:03 PM
set and export using bash script acummings Linux - General 10 01-03-2005 02:22 PM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 07:47 AM
BASH variable export Barbarian Programming 2 11-27-2001 08:37 PM


All times are GMT -5. The time now is 12:18 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration