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 06-13-2011, 08:23 AM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,648

Rep: Reputation: 51
foo1=$(cat foo2) does not work as expected (by me).


Kernel 2.6.21.5

Hi:
Code:
bash-3.1$ cat radios
    Antena 2: mms://195.245.168.21/antena2 (para mplayer).
    Arnold Schoenberg: http://81.223.24.100:8000/listen.pls
bash-3.1$ u12=$(cat radios)
bash-3.1$ echo $u12
 Arnold Schoenberg: http://81.223.24.100:8000/listen.pls
bash-3.1$
Why does not the whole output of 'cat radios' go into u12? I suspect it has something to do with the newline char, but do not exactly understand why it fails. Thanks.
 
Old 06-13-2011, 08:41 AM   #2
segmentation_fault
Member
 
Registered: Sep 2008
Location: Ioannina, Greece
Distribution: Gentoo
Posts: 332

Rep: Reputation: 55
I suspect this is because you store it in a variable. So this is translated like this:
Code:
u12=Antena 2: mms://195.245.168.21/antena2 (para mplayer). <enter>
(now u12 contents are "Antena 2: mms://195.245.168.21/antena2 (para mplayer)."

u12=Arnold Schoenberg: http://81.223.24.100:8000/listen.pls <enter>
(now the contents of u12 are "Arnold Schoenberg: http://81.223.24.100:8000/listen.pls"
So you need to figure out how to treat the newline character in a different way. Also if you can get the work done with a file you can use a file instead of a variable. eg
Code:
cat radios > u12
 
Old 06-13-2011, 08:48 AM   #3
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
You have something wrong with the radios file.
When I try to repeat this it works, but not as you would like, I suspect.
Code:
[smoker@kids radio]$ cat radios
        Antena 2: mms://195.245.168.21/antena2 (para mplayer).
        Arnold Schoenberg: http://81.223.24.100:8000/listen.pls
[smoker@kids radio]$ ul2=$(cat radios)
[smoker@kids radio]$ echo $ul2
Antena 2: mms://195.245.168.21/antena2 (para mplayer). Arnold Schoenberg: http://81.223.24.100:8000/listen.pls
[smoker@kids radio]$
Did you create the radios file on a windows machine ?
If so you need to edit it and remove the breaks and re-enter them.
I created my radios file using vi.

Also, try using double quotes :
Code:
[smoker@kids radio]$ echo "$ul2"
        Antena 2: mms://195.245.168.21/antena2 (para mplayer).
        Arnold Schoenberg: http://81.223.24.100:8000/listen.pls
[smoker@kids radio]$

Last edited by smoker; 06-13-2011 at 08:56 AM.
 
Old 06-13-2011, 08:51 AM   #4
raj77_in
LQ Newbie
 
Registered: Oct 2008
Posts: 26

Rep: Reputation: 15
what is value of IFS?

Quote:
Originally Posted by stf92 View Post
Kernel 2.6.21.5

Hi:
Code:
bash-3.1$ cat radios
    Antena 2: mms://195.245.168.21/antena2 (para mplayer).
    Arnold Schoenberg: http://81.223.24.100:8000/listen.pls
bash-3.1$ u12=$(cat radios)
bash-3.1$ echo $u12
 Arnold Schoenberg: http://81.223.24.100:8000/listen.pls
bash-3.1$
Why does not the whole output of 'cat radios' go into u12? I suspect it has something to do with the newline char, but do not exactly understand why it fails. Thanks.
Can you check the value of IFS. A newline would be translated to space when you do something like this but should still contain all the lines of the file.
also try with bash -x to see what is happening along with trying to see if the values were stored in array by printing the size of u12 as
Code:
echo ${#u12}
 
Old 06-13-2011, 08:59 AM   #5
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,648

Original Poster
Rep: Reputation: 51
Yes. Just after posting I suspected the file from where radio came from could have <CR><LF> line terminators. I used vim's 'set ff=unix' command to remove the carriage return chars, and hexdump showed the <CR>s where gone.

After that, I get the same output as you for the command 'echo $u12' and, yes, I would like the <CR> to remain. Thanks a lot.

EDIT
*** I overlooked the rest of smoker's post. Sorry.

Last edited by stf92; 06-13-2011 at 09:02 AM.
 
Old 06-13-2011, 09:01 AM   #6
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
See my edit about double quotes.
 
1 members found this post helpful.
Old 06-13-2011, 09:10 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
+1 to smoker's idea that the problem in in the file, not in the bash commands and that you need to double quote the variable when echoing it. I don't believe $IFS is relevant except when echoing $u12 without quotes. Here's experimenting with IFS:
Code:
c@CW8:/tmp$ IFS=e
c@CW8:/tmp$ u12=$(cat radios)
c@CW8:/tmp$ echo "$u12"
    Antena 2: mms://195.245.168.21/antena2 (para mplayer).
    Arnold Schoenberg: http://81.223.24.100:8000/listen.pls
c@CW8:/tmp$ echo $u12
    Ant na 2: mms://195.245.168.21/ant na2 (para mplay r).
    Arnold Scho nb rg: http://81.223.24.100:8000/list n.pls
c@CW8:/tmp$ unset IFS
c@CW8:/tmp$ echo $u12
Antena 2: mms://195.245.168.21/antena2 (para mplayer). Arnold Schoenberg: http://81.223.24.100:8000/listen.pl
Note: in bash, unsetting IFS is equivalent to it having its default value.
 
Old 06-13-2011, 09:12 AM   #8
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,648

Original Poster
Rep: Reputation: 51
I'm reading it. I double quoted and, same output as you. I beleave the syntax for bash is far more complicated than C's syntax. Let us keep reading.
 
Old 06-13-2011, 09:19 AM   #9
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 3,648

Original Poster
Rep: Reputation: 51
Yes, two posts instead of one. But editing the last one I do not know if makes people take notice as much as a new one.

Well, catkin, and what is this IFS after all? In the meantime, I'll try to figure out its meaming from bash man.

EDIT
*** In bash, it's a shell variable, whose meaning is
*** very well explained (manual).

Last edited by stf92; 06-13-2011 at 09:25 AM.
 
Old 06-13-2011, 09:35 AM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
IFS is well documented but it is not always intuitively obvious in which situations it is relevant.
 
Old 06-13-2011, 09:48 AM   #11
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Try this:

Code:
u12="$(cat radios)"
echo "$u12"
Use double quotes to preserve whitespace. Otherwise, the items will be split into an array using whitespace as the separator.

Last edited by MTK358; 06-13-2011 at 09:50 AM.
 
Old 06-13-2011, 10:19 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by MTK358 View Post
Otherwise, the items will be split into an array using whitespace as the separator.
[PEDANTRY]More precisely: using a space as the separator[/PEDANTRY]

EDIT: even more precisely (!) the value will be split into words everywhere it contains one or more characters that are in $IFS and reassembled with the words separated by a space:
Code:
c@CW8:/tmp$ IFS='
un'
c@CW8:/tmp$ x='Linux Questions
> Rules OK'
c@CW8:/tmp$ echo $x
Li  x Q estio s R les OK
c@CW8:/tmp$ echo "$x"
Linux Questions
Rules OK

Last edited by catkin; 06-13-2011 at 10:28 AM.
 
1 members found this post helpful.
  


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
Getting a INIT script to work as expected bombshell Programming 2 02-24-2011 01:55 AM
Linking does not work as expected Setya Ubuntu 4 12-03-2008 01:05 PM
Script cd not work as expected cdcshu Linux - Software 1 11-10-2007 05:38 AM
If your hardware does not work as expected! Keruskerfuerst SUSE / openSUSE 1 11-06-2005 06:43 AM
-ne argument expected WHY doesnt this work VisionZ Linux - Newbie 8 03-27-2004 01:39 AM

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

All times are GMT -5. The time now is 04:17 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration