Register
It is currently Tue Jul 29, 2014 8:46 am

Passing strings into ssh commands. [SOLVED]


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Fri Aug 14, 2009 2:34 pm   

Joined: Sat May 30, 2009 5:56 am
Posts: 15
I have a simple script for ssh-ing messages to my wife through the Ubuntu notification system (notify-osd):
Code:
#!/bin/bash
# Send a message through ssh to notify-osd on a remote host.
#

ssh -X -p #### username@remote-host DISPLAY=:0 notify-send $1
exit


Now, the $1 is the problem I'm having, and I'll get to that in a second.

I should say first that this command works as an alias in the remote-host, but with the ssh part stripped off, such that:
Code:
alias chat='DISPLAY=:0 notify-send'
After SSH-ing to the remote machine, type 'chat' at a prompt, and enter your message "surrounded by double quotes" to have it appear as a notification bubble on the remote screen.

In script form, without the $1 variable there, I get an error, "No summary specified." With the $1 variable, if I type more than one word separated with a space, I get the error "Invalid number of options." A little research led me to believe that if I substituted in $@ instead of $1, it might work better, but it doesn't.

Seems to me that there has to be a variable there - what should I use?


Last edited by uncertain on Sat Aug 15, 2009 7:19 am, edited 1 time in total.

Top
 Profile  
 PostPosted: Fri Aug 14, 2009 5:54 pm   

Joined: Sat May 30, 2009 5:56 am
Posts: 15
An update of sorts:From my terminal (not the script) it will work like this
Code:
ssh -X user@host 'DISPLAY=:0 notify-send "This is a test."'



I don't even know how to ask any more. Wrapping the DISPLAY & notify-send commands in single quotes makes it functional. How can I make the double-quoted text a variable that I can use in a command prompt; e.g., chat "This isn't just a test any more." and have it pass through to notify-send on the remote host?


Top
 Profile  
 PostPosted: Fri Aug 14, 2009 7:03 pm   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
Quote:
An update of sorts:From my terminal (not the script) it will work like this
Code:
ssh -X user@host 'DISPLAY=:0 notify-send "This is a test."'


What happens when you run that in the script?

Would it work if you have some thing like this?
Code:
msg='This is a test'    # Or however you want to get the message in there
ssh -X user@host DISPLAY=:0 notify-send \'$msg\'

The message needs to be enclosed in quotes (either single or double, I think) for notify-send. But, you need to escape them in the script.


Top
 Profile  
 PostPosted: Sat Aug 15, 2009 1:43 am   

Joined: Sat May 30, 2009 5:56 am
Posts: 15
I get the same errors if I try to make the msg='$1' or msg='$@' variables. Again, with $1, I can send single-word messages, or whole lines of text as long as I don't use any spaces.

I see what you're saying, though. But having the message included in the script itself and having to open, edit, save, then execute it every time I use it is actually a little worse than having to ssh to the other computer and the execute the command locally.

I'll see what I can come up with along these lines.


Top
 Profile  
 PostPosted: Sat Aug 15, 2009 7:19 am   

Joined: Sat May 30, 2009 5:56 am
Posts: 15
The final, working copy, as such
Code:
    #!/bin/bash
    #
    #Send messages through SSH to remote hosts' notify-osd
    #
    message="$@"
    ssh -X user@host "DISPLAY=:0 notify-send \"$message\""
    exit


Which is more or less how you showed me above. I guess I was just cross-eyed after staring at it and googling for so long.

Thanks for the help!


Top
 Profile  
 PostPosted: Sun Aug 16, 2009 1:10 am   
User avatar

Joined: Sat Jun 13, 2009 8:53 pm
Posts: 73
Location: Texas!
I'm glad it's working for you.

The important lesson to take home here is the difference between using double quotes, single quotes, or no quotes.

As a general rule, double quotes allow for interpolating, which is the expansion of variables, among other things. That's why message="$@" works for you, but message='$@' would not.

Single quotes do not allow for interpolation. Since I didn't have a variable in my string, I just used single quotes.

The man page for notify-send is nearly useless, but I assumed that it takes a string as an argument (one argument). If the string is not enclosed in quotes, then words separated by spaces will look like multiple arguments; we enclose them in quotes (single or double) to make it a single string. A more subtle note is that we escape the quotes so that bash doesn't interpret them in the script, and just passes normal quotes to notify-send.

It's all a little tricky, but hopefully this cleared up some issues for you.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP