Visit Our UNIX and Linux User Community


Bash script puts \r at the end of variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script puts \r at the end of variable
# 1  
Old 02-13-2013
Bash script puts \r at the end of variable

I'm pretty new to bash scripts and I'm trying to work through some issues. Would appreciate any suggestions.

I have a list of servers in a text file (I used the FQDN's), I assign the file name to a variable, I then use cat to read the list of file names and echo them to the screen. But when it assigns the variable it seems to append a \r to the end of the variables value. Although the \r doesn't show up unless I use the set command to debug. As you can see from code below, I'm trying to use a server list to perform a secure copy of my password files so that I don't have to log on to each server. Here is the code snippet in question:

Code:
#!/bin/bash
#
set -x

FILENAME=server_list.txt

cat $FILENAME | while read LINE
do
        echo "$LINE"
#       mkdir /sysadmin/bin/passwd_audit/$LINE
#       scp root@$LINE:/etc/passwd /sysadmin/passwd/$LINE-passwd.txt
#       scp root@$LINE:/etc/group /sysadmin/passwd/$LINE-group.txt
#       scp root@$LINE:/etc/sudoers /sysadmin/passwd/$LINE-sudoers.txt

done

set +x

Here are the first few lines in the file:

Quote:
api01r5v.lamar.edu
appworxdbdev.lamar.edu
appworxdbprod.lamar.edu
appwxdbdevr5p.lamar.edu
Here's what the output from the script looks like:

Quote:
[root@netbkpmaster passwd_audit]# ./getinfo_script
+ FILENAME=server_list.txt
+ cat server_list.txt
+ read LINE
+ echo $'api01r5v.lamar.edu\r'
api01r5v.lamar.edu
+ read LINE
+ echo $'appworxdbdev.lamar.edu\r'
appworxdbdev.lamar.edu
+ read LINE
+ echo $'appworxdbprod.lamar.edu\r'
appworxdbprod.lamar.edu
+ read LINE
+ echo $'appwxdbdevr5p.lamar.edu\r'
appwxdbdevr5p.lamar.edu
Is this normal behavior? I've used this same script in the past and it's worked fine for me. Not sure why it changed suddenly. Any suggestions would be greatly appreciated.
# 2  
Old 02-13-2013
Your data file was probably edited on windows, a \r on the end of each line is standard in MSDOS.

You can use
Code:
$ dos2unix server_list.txt

to remove these \r chars.
# 3  
Old 02-13-2013
Chubler, you rock. That did the trick, thank you so much.

Previous Thread | Next Thread
Test Your Knowledge in Computers #504
Difficulty: Medium
Typically, programs operate in a reverse-linear flow of control.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

2. Shell Programming and Scripting

Script that gathers specific values from files and puts it into HTML to send e-mail

Hi All, Sorry for long topic here. So the drill goes like that, I need a script which gathers different values from different files/locations. A_CT=`cat a.dat | awk -F'|' '{print $1}' >> report.txt` B_CT=`cat b.dat | awk -F'|' '{print $3}' >> report.txt` C_CT=`cat c.dat | awk -F'|'... (4 Replies)
Discussion started by: shivakid
4 Replies

3. Shell Programming and Scripting

Acces Variable from expect-Script in bash-Script

Hi all, I have a little problem with a expect in a bash Script. The hull of my script: #!/bin/sh ( expect -c ' set a \"eee\"; # the variable a ' ) echo $a; # using the variable out of the expect script I would like to use the variable out of the expect script(in bash),... (3 Replies)
Discussion started by: gandalfthepink
3 Replies

4. Shell Programming and Scripting

Bash script having variable substitution problems

Hi I am setting the variables like this : setenv MODULE1 modem5__3 setenv MODULE2 modem5__2 setenv MODULE3 modem_ctrl_1_1 setenv MODULE4 modem_1_0 setenv COUNT 10 I am having a bash script as shown below ################################################ #!/bin/bash for ((... (5 Replies)
Discussion started by: kshitij
5 Replies

5. Shell Programming and Scripting

script puts;; I need just ;

Hello Folks, I must be blind that I cant figure out this perl script why I end up with ;; at the end of the line. I am not an expert by any means #!/usr/bin/perl use File::Find; open F,shift or die $!; my %ip=map/(\S+)\s+(\S+)/,<F>; close F; find sub{ if( -f ){ local... (4 Replies)
Discussion started by: richsark
4 Replies

6. Shell Programming and Scripting

script to detect a file from inserted usb and puts into a Variable

There is a same named log file that I have on my 2 different android phones. When I plug it into my computer, it appears in the media folder, For example the first android phone: /media/F6BA-0AF5/folder/A.log I want to put that into a variable to be manipulated.... (3 Replies)
Discussion started by: tobenguyen
3 Replies

7. Shell Programming and Scripting

end of file error bash ftp script

Hello kind programmers :) I am a newbie and running into an error "line 28: syntax error: unexpected end of file" on the script shown below. Any help would be greatly appreciated. Thanks! #! /bin/bash if ($#argv <3) then echo 'Usage get_modis_snow ' echo 'ftp script for MYD10A2... (2 Replies)
Discussion started by: cmshreve
2 Replies

8. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies

9. Shell Programming and Scripting

Puts command in a script

Hi, I am trying to output a set of fields to a file: set outFile "printHeader.tmp" set dbFields "REPORT.TIMESTAMP.s,REPORT.CUSTOMER.s,REPORT.CODE.s" puts $dbFields > $outFile I receive the following error message: bad argument "printHeader.tmp": should be "nonewline" ... (2 Replies)
Discussion started by: MrG-San
2 Replies

10. Shell Programming and Scripting

How do you parse a variable in a bash script?

I have a script I use on my web server (Apache2). I am changing to Lighttpd and need to make a few changes. This is what I use on my apache server #!/bin/bash # accepts 3 parameters: <domain name> <user name> <XXXXXXXX> # domain name is without www (just domain.com) # username would be... (3 Replies)
Discussion started by: vertical98
3 Replies

Featured Tech Videos