LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   foo1=$(cat foo2) does not work as expected (by me). (https://www.linuxquestions.org/questions/linux-newbie-8/foo1%3D%24-cat-foo2-does-not-work-as-expected-by-me-886034/)

stf92 06-13-2011 07:23 AM

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.

segmentation_fault 06-13-2011 07:41 AM

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

smoker 06-13-2011 07:48 AM

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]$


raj77_in 06-13-2011 07:51 AM

what is value of IFS?
 
Quote:

Originally Posted by stf92 (Post 4384101)
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}

stf92 06-13-2011 07:59 AM

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.

smoker 06-13-2011 08:01 AM

See my edit about double quotes.

catkin 06-13-2011 08:10 AM

+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.

stf92 06-13-2011 08:12 AM

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.

stf92 06-13-2011 08:19 AM

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).

catkin 06-13-2011 08:35 AM

IFS is well documented but it is not always intuitively obvious in which situations it is relevant.

MTK358 06-13-2011 08:48 AM

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.

catkin 06-13-2011 09:19 AM

Quote:

Originally Posted by MTK358 (Post 4384176)
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



All times are GMT -5. The time now is 02:07 PM.