LinuxQuestions.org
Help answer threads with 0 replies.
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
  Search this Thread
Old 04-25-2013, 03:02 AM   #1
slowerogue
Member
 
Registered: Oct 2012
Posts: 96

Rep: Reputation: Disabled
script read input from file


hi guys
i want the variable GN has the input from file
Code:
GN="/tmp/scripts/groupname"
jailpath="/data/$GN"

cat ${GN} | \
while read GN2
do
   GN=${GN2}
   echo $GN
done


echo $GN

the echo $GN does show what i want, but only before the "done"
when i echo $GN outside the while-do thing, it goes back to original,means my jailpath cannot get the correct value

any helps on how to solve this? ><
thanks
 
Old 04-25-2013, 03:50 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,374

Rep: Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383Reputation: 2383
Try reading the file from the bottom of the loop http://stackoverflow.com/questions/8...e-line-by-line.
Your one opens a new shell (& env) due to the pipe.
 
Old 04-25-2013, 05:03 AM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
As Chris said, try this:
Code:
#!/bin/bash
GN="/tmp/scripts/groupname"
jailpath="/data/$GN"

while read -r GN2
do
   GN=${GN2}
   echo $GN
done < ${GN}

echo $GN
 
1 members found this post helpful.
Old 04-25-2013, 02:30 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960Reputation: 1960
Another description for you:

I set variables in a loop that's in a pipeline. Why do they disappear after the loop terminates? Or, why can't I pipe data to read?

http://mywiki.wooledge.org/BashFAQ/024

Using a redirection for the file input also avoids a Useless Use Of Cat.


And please lose the pointless curly brackets around the variables, ok? They do nothing but clutter up the code. Save the full variable form for when you really need it.

Last edited by David the H.; 04-25-2013 at 02:31 PM.
 
Old 04-25-2013, 11:26 PM   #5
slowerogue
Member
 
Registered: Oct 2012
Posts: 96

Original Poster
Rep: Reputation: Disabled
thanks guys, it worked
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script to read multiple user input prasunjit Programming 1 03-31-2013 02:09 PM
shell script read input from file rmugunthan Linux - Newbie 9 06-18-2012 07:45 PM
shell script having multiple grep statements-I want input file to be read only once mukta9003 Linux - Newbie 4 08-27-2008 12:58 AM
how to read more than ten arguments of user input in a script new_2_unix Linux - Newbie 5 11-19-2007 03:18 PM
shell script to read input from csv ip addresses? kr0m3 Programming 3 07-21-2007 08:51 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:30 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration