LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 08-21-2006, 01:01 PM   #1
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 491

Rep: Reputation: 75
issue with variable in bash script


Hi there,

I've the following issue with one of my script.
Here is the script:
Code:
#! /bin/bash

for filename in $( cat /path_of_the_file/list_a ); do
echo FileVersion /path_of_the_file/${filename}.ver.tmp 
done
exit

the file list_a look like this:
Code:
myfile1.dll
myfile2.dll
ect...
but when i execute my script in get the following output:
Code:
./myscript
.ver.tmpion /path_of_the_file/myfile1.dll
But if i type the command line echo FileVersion /path_of_the_file/myfile1.dll.ver.tmp in the command line with out using any variable it work fine and i get the following output:
Code:
echo FileVersion /path_of_the_file/myfile1.dll.ver.tmp
FileVersion /path_of_the_file/myfile1.dll.ver.tmp


I don't understand why the expression .var.tmp is displayed at the beginning of the line.


I have try to escape the dot like this:
Code:
#! /bin/bash

for filename in $( cat /path_of_the_file/list_a ); do
echo FileVersion /path_of_the_file/${filename}\.ver\.tmp 
done
exit
But it's the same issue.
Any idea???

Last edited by angel115; 08-21-2006 at 01:05 PM.
 
Old 08-21-2006, 01:07 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
It sounds more like bash is wraping the output - does it look almost right with:
echo FileVersion /${filename}.ver.tmp
 
Old 08-21-2006, 01:09 PM   #3
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 46
It looks very much like list_a contains the $'\r' (Carriage Return) character, which "rewinds" to the beginning of the line.

The reason it contains that character is that it's written for MS-DOS, on which newlines are represented by $'\r\n', whereas on unix they're represented by $'\n'.

To get rid of them, do this
Code:
$ sed -i -e 's/\r$//g' /path/file
If you want to see them, try
Code:
$ tr '\r' '#' < file
Also, try playing around with ascii(1). Also, understand what the magic variable IFS does.
 
1 members found this post helpful.
Old 08-21-2006, 01:11 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

The script (first example) works fine, it's the infile (list_a) that is the problem.

It's probably not a normal text file, but a dos/windows file:

$ file list_a
list_a: ASCII text, with CRLF line terminators


Instead of:
$ file list_a
list_a: ASCII text


I use vi to change file from one type to another (there are other ways as well). After loading file in vi do the following:
<esc>
:set ff=unix
:wq

Use the file command to see if it worked.
 
Old 08-21-2006, 01:42 PM   #5
angel115
Member
 
Registered: Jul 2005
Location: France / Ireland
Distribution: Debian mainly, and Ubuntu
Posts: 491

Original Poster
Rep: Reputation: 75
Thumbs up

Quote:
Originally Posted by jonaskoelker
It looks very much like list_a contains the $'\r' (Carriage Return) character, which "rewinds" to the beginning of the line.

The reason it contains that character is that it's written for MS-DOS, on which newlines are represented by $'\r\n', whereas on unix they're represented by $'\n'.

To get rid of them, do this
Code:
$ sed -i -e 's/\r$//g' /path/file
If you want to see them, try
Code:
$ tr '\r' '#' < file
Also, try playing around with ascii(1). Also, understand what the magic variable IFS does.
Thanks jonaskoelker,

You are the one, i used you sed script to clean my source file and now every thing is working fine.



Thanks again;
Angel115.
 
  


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
How to export environment variable from a bash script dimaash Linux - Newbie 20 08-09-2011 07:12 AM
sed doesn't accept $variable in bash script chii-chan Programming 6 05-28-2005 07:07 AM
bash script to replace char in variable gmartin Programming 5 11-08-2004 01:46 PM
bash script $n variable expansion cortez Programming 6 12-08-2003 04:03 PM
Bash script renaming a variable zael Programming 3 09-30-2003 04:37 AM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration