LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-05-2013, 08:14 PM   #1
smallnokia
LQ Newbie
 
Registered: Jun 2013
Posts: 5

Rep: Reputation: Disabled
Unhappy in bash script the export variable can't pass to command line


Dear all,

In my bash scripting, I want to pass my variable to awk command, like below:

#!/bin/sh

...
export FLINE=1
FILE_NAME=`awk 'NR==$FLINE' my_file.f`
echo $FILE_NAME

==================================
when I execute it, echo output is empty. The problem is that $FLINE value is not passed to awk command because when I replace $FLINE to real number "1", output is correct.

Anybody can help how to solve it ?

Best wishes
 
Old 06-05-2013, 09:18 PM   #2
smallnokia
LQ Newbie
 
Registered: Jun 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Wink

Sorry my mistake, I should use double quote instead of single. ...

question is solved now
 
Old 06-06-2013, 01:10 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Please use thread tools to mark as SOLVED
 
Old 06-06-2013, 02:18 AM   #4
smallnokia
LQ Newbie
 
Registered: Jun 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Cool

Quote:
Originally Posted by grail View Post
Please use thread tools to mark as SOLVED

thanks for your reminder :-)
 
Old 06-06-2013, 02:38 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques. Thanks.


A better technique is to import the shell variable into an awk variable.

And you don't need to export a variable unless some sub-process needs to access it in their own environment. Exporting is only rarely needed in shell scripts.

Also, $(..) is highly recommended over `..`

Code:
FLINE=1
FILE_NAME="$( awk -v "lnum=$FLINE" 'NR==lnum' my_file.f )"
echo "$FILE_NAME"
But finally, sed is usually easier to use than awk when extracting specific line numbers.

Code:
FILE_NAME="$( sed -n "$FLINE {p;q}" my_file.f )"
 
1 members found this post helpful.
Old 06-06-2013, 09:27 PM   #6
smallnokia
LQ Newbie
 
Registered: Jun 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Smile

Hi @David the H.

Thanks very much for your suggestions.

Yes my problem was actually not export issue, it was quote issue. so without export it also works. (we don't need to export anyway).

in your post, you recommend "$()" over "` `", could you explain a little bit why it is better? Is it code performance?

I tried your alternative way of using sed also.

Best wishes,
 
Old 06-07-2013, 02:26 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Did you click on the link?
 
Old 06-07-2013, 02:49 AM   #8
smallnokia
LQ Newbie
 
Registered: Jun 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Did you click on the link?

oh... sorry .. I am not aware that is a link ...
 
  


Reply



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



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
In BASH shell, how do I export a variable from a script. MichaelG67 Linux - Software 2 06-24-2010 01:03 PM
How to pass a c variable to a bash script? daYz Programming 3 09-28-2007 08:30 AM
cannot export result from awk into a variable in a bash script Emmanuel_uk Linux - Newbie 4 03-07-2005 01:54 AM
How do I pass a C variable to a Bash command ? Linh Programming 6 07-07-2003 03:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:10 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration