1 (one) line if then statement while ssh to remote server

Discussion in 'Programming/Scripts' started by alexus, Jan 4, 2007.

  1. alexus

    alexus New Member

    does anyone knows how i can do if then statment in 1 (one) line when I ssh to remote server?
     
  2. alexus

    alexus New Member

    and/or

    does anyone know how you can run multiple line script through single connection via ssh?
     
  3. falko

    falko Super Moderator Howtoforge Staff

    You don't need to put it in one line. Just paste your shell code into the shell and hit enter.
     
  4. alexus

    alexus New Member

    actually .. i _do_ need it in 1 line
     
  5. martinfst

    martinfst Member Moderator

    Can you use the line continuation character
    Code:
    \
    ?
    Put this as the last char and the shell will wait for a next line. But I'm not sure I understand your question correctly.
     
  6. alexus

    alexus New Member

    what i want my script to do is:

    to ssh to a remote server
    check sshd_config for AuthorizedKeysFile
    check if there is ~/.ssh
    copy public key from original host ~/.ssh/*pub to a remote host ~/`grep AuthorizedKeysFile /etc/ssh/sshd_config | awk '{print $2}'`

    and i dont want to type password 2-3 times, i want this to be done in one line so i only have to type password once
     
  7. falko

    falko Super Moderator Howtoforge Staff

    You can do it like this:
    Code:
    ssh user@hostname "command1 && command2 && command3 [...]"
     
  8. alexus

    alexus New Member

    what if i already use " and ' ?
     
  9. martinfst

    martinfst Member Moderator

    Escape your own special characters by placing a '\' in front of each to be escaped character, so something like
    Code:
    ssh user@hostname "command1 --arg1 \"xx\" && command2 && command3 [...]"
     
  10. alexus

    alexus New Member

    didn't work for me for some reason before but not it did :)
     
  11. h2oski

    h2oski New Member

    You can do if/for ect.. statements on one line by separating statements with a semi-colon

    Code:
     if [ -f tmp.txt ] ;then echo "yep.. that is a file";fi
    
    same thing when doing a for statement on one line.

    Code:
    for i in 1 2 3; do echo $i;done
    
    
     

Share This Page