2

To boil it down: The following command in bash

echo $(ssh -t pi@doctor "sudo stat -c \"%U\" /var/www/candy/example/index.html")101

Gives the this result

Connection to doctor closed.
101-data

While

echo $(ssh -t pi@doctor "sudo stat -c \"%U\" /var/www/candy/example/index.html")

gives

Connection to doctor closed.
www-data

Why is this?

Background: I'm using this script (called ced.sh):

for ARG; do
        name=${ARG##*/}
        echo name=$name
        path=${ARG%/*} 
        echo path=$path
        owner=$(echo $(ssh -t $remote_user@$server "sudo stat -c \"%U\" $ARG"))
        echo owner=$owner
        group=$(echo $(ssh -t $remote_user@$server "sudo stat -c \"%G\" $ARG"))
        echo group=$group
        perms=$(echo $(ssh -t $remote_user@$server "sudo stat -c \"%a\" $ARG"))
        echo perms=$perms
        read -p "Gibt es einen Dienst/Server, der auf diese Datei zurückgreift (notfalls leer lassen)? " service
        echo service=$service
        echo "name="\"$name\" "path="\"$path/\" "owner="\"$owner\" "group="\"$group\" "perms="$perms "service="$service
done

Its purpose is to read some information (e.g. owner, group, permissions) from files on a server, which I'd like to edit locally. The last line should produce an output that can be stored in a file.

It would have been rewritten to:

echo "name="\"$name\" "path="\"$path/\" "owner="\"$owner\" "group="\"$group\" "perms="$perms "service="$service >> "$HOME/...bla bla..."

The output looks like this:

Software/EigeneProgramme/ced.sh -p /var/www/candy/example/index.html
name=index.html
path=/var/www/candy/example
Connection to doctor closed.
owner=www-data
Connection to doctor closed.
group=www-data
Connection to doctor closed.
perms=644
Gibt es einen Dienst/Server, der auf diese Datei zurückgreift (notfalls leer lassen)? apache
service=apache
 service=apacheta path="/var/www/candy/example/" owner="www-data

I don't understand why echo produces the last line. It seems that it is writing to line in on. Did I forget to escape something?

3

There is an easy explanation for this. The values retrieved from the remote server all come with a trailing carriage return (\r).

You were expecting this:

www-data101

name="index.html" path="/var/www/candy/example/" owner="www-data" group="www-data" perms=644 service=apache

But www-data and 644 are followed by a carriage return. If CR would be rendered as a newline by your terminal emulator, then the output would look like this:

www-data
101

name="index.html" path="/var/www/candy/example/" owner="www-data
" group="www-data
" perms=644
 service=apache

Without the linefeeds, lines overlap; the dots represent the characters that are overwritten.

...-data
101

................. path="/var/www/candy/example/" owner="www-data
...............ta
...........
 service=apache

As you already pointed out, on most terminals this looks like:

101-data

 service=apacheta path="/var/www/candy/example/" owner="www-data

You can verify this by piping the remote output to cat -v; the carriage return will show up as ^M.

ssh -t pi@doctor "sudo stat -c \"%U\" /var/www/candy/example/index.html" | cat -v

One possible solution is to strip off the carriage return by piping the remote output through tr -d '\r':

ssh -t pi@doctor "sudo stat -c \"%U\" /var/www/candy/example/index.html" | tr -d '\r'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.