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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
03-04-2005, 02:57 AM
|
#1
|
Senior Member
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606
Rep:
|
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
|
|
|
03-04-2005, 05:14 AM
|
#2
|
Member
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507
Rep:
|
Hi,
Backticks should do it...
myshortvar=`gawk 'BEGIN { print substr("'$myvar'",3) }'`
John
|
|
|
03-04-2005, 06:40 AM
|
#3
|
Senior Member
Registered: Jul 2004
Location: France
Distribution: Arch Linux
Posts: 1,897
Rep:
|
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.
|
|
|
03-04-2005, 07:55 AM
|
#4
|
Senior Member
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606
Original Poster
Rep:
|
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
|
|
|
03-07-2005, 02:54 AM
|
#5
|
Senior Member
Registered: Nov 2004
Distribution: Mandriva mostly, vector 5.1, tried many.Suse gone from HD because bad Novell/Zinblows agreement
Posts: 1,606
Original Poster
Rep:
|
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
}
|
|
|
All times are GMT -5. The time now is 03:28 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|