That gets rid of the error "/bin/timeline: line 49: /media/USB: No such file or directory" but the temp variable is still null when I run the script :(
Main Topics
Browse All TopicsI am new to bash scripting and linux. I have a bash script that I have been working on that will take a path to a dd disk image file and pass it into fdisk to parse out some information. The issue that I am having is after I use sed to get the text that I want, I can't figure out how to save it into a variable. Instead I just keep getting an error:
"/bin/timeline: line 49: /media/USB: No such file or directory"
This error appears to originate from the line where I set the temp variable:
temp=`$line|sed "s/.*\.001p[[:digit:]][^[:
I have tried surrounding the command in bacticks like above and with $() but I still get the same error message. Below is what the output looks like from running the command:
[root@SIFTWorkstation /]# timeline /media/USB\ 500GB/Images/6RX99BCP/6RX9
--------------------------
/media/USB 500GB/Images/6RX99BCP/6RX9
--------------------------
63
/bin/timeline: line 49: /media/USB: No such file or directory
count = 1
--------------------------
/media/USB 500GB/Images/6RX99BCP/6RX9
--------------------------
109980990
/bin/timeline: line 49: /media/USB: No such file or directory
count = 2
partition_offsets[0] = 1
temp =
[root@SIFTWorkstation /]#
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Ok, I can see the temp variable now, but I have defined partition_offsets outside of the loop and if i assign the output to "partition_offsets[$count]
It appears that if you use a loop fed by a piped command the loop will perform within a sub-shell which is why I am seeing this issue.
http://www.nucleardonkey.n
http://nion.modprobe.de/bl
I have included my modified script. I changed it to make use of the partition_offsets array rather than the temp variable. Below is the output.
[root@SIFTWorkstation /]# timeline /media/USB\ 500GB/Images/6RX99BCP/6RX9
--------------------------
/media/USB 500GB/Images/6RX99BCP/6RX9
--------------------------
63
count = 0
partition_offsets[0] = 63
--------------------------
/media/USB 500GB/Images/6RX99BCP/6RX9
--------------------------
109980990
count = 1
partition_offsets[1] = 109980990
-----outside of loop-----
partition_offsets[0] =
[root@SIFTWorkstation /]#
I have tried changing this to store the fdisk and grep command results to a variable and do a "for line in $lines" style of loop but the loop didn't appear to treat the result of the grep as multiple lines like the current while loop that I am using.
This is a common problem with bash (Posix shell usually gets it right, and remembers the varialevalues outside the loop),
There are a couple of solutions. One is to use the "exec" command. Put the data you want to read in a file. Then run "exec" using that file as standard input. The "while" loop then reads from that file:
fdisk -ul "$1" 2>/dev/null | grep "6RX99BCP\.001p" > /tmp/tmpfile.$$
exec < /tmp/tmpfile.$$
while read line
do
echo "-------------------------
echo "$line"
echo "-------------------------
and so on. Delete /tmp/tmpfile.$$ when the loop finishes.
The other way is to store variables in files (e.g "echo $count > /tmp/tmpcount") within the loop, then read the files outside the loop. This works fine for simple variables, but would be very difficult for your arrays. The "exec" method is the way to do it here.
I have raised the points to 500 to split between you two.
amit_q: I have tried your suggestion but it still printed out nothing. just "partition_offsets = "
simon3270: the exec command seems to do the trick although I don't fully understand why it works. If you would be kind enough to explain why or at least point me to a good source of information on this subject I would apreciate it.
The "exec" line says that from that point on in the script, standard input comes from the file you specify, not from the keyboard. Then the "read line" bit, which would normally read from standard input, reads from the file instead.
If your script needed the real standard input later on, there is a way to remember the actual stdin, and reset it after you have read the file (see link below).
I'm not sure where I learnt this - it's just one of the things I have picked up over the years - but I did find a good description on http://www.linuxtopia.org/
Thanks for the points - made me up to a Qualified Expert!
Business Accounts
Answer for Membership
by: amit_gPosted on 2009-10-30 at 08:25:59ID: 25703896
temp=`$line|sed "s/.*\.001p[[:digit:]][^[: digit:]]\{ 0,\}\([[:d igit:]]\{1 ,\}\).*/\1 /"` digit:]]\{ 0,\}\([[:d igit:]]\{1 ,\}\).*/\1 /"`
should instead be
temp=`echo $line|sed "s/.*\.001p[[:digit:]][^[: