Jump to content

Help with Bash Scripting


Recommended Posts

I am practicing bash scripting and have created the following script which unfortunatly does not work.

 

#!/bin/bash
#
# runssh - A Script to start and initialise either an ssh or sftp command
#
# Graham Hadgraft

shopt -s -o nounset

declare USERNAME # username e.g. u******
declare COMMAND # run ssh or sftp

# Instructions

printf "%s\n" "Press the number corresponding to the program you wish to use:"
printf "%s\n" "1. SSH"
printf "%s\n" "2. SFTP"

# Main Script

read -p "Enter your choice:" COMMAND
read -p "Enter your username:" USERNAME

if ["$COMMAND" = "1"]; then
ssh -l $USERNAME ssh.ee.port.ac.uk
fi

if ["$COMMAND" = "2"]; then
sftp $USERNAME@ssh.ee.port.ac.uk
fi

 

I am trying to make it so when i press 1 it runs the ssh command. However the if command does not seem to evaluate the contents of Command. Can anyone tell me what is wrong with this script.

Link to comment
Share on other sites

From "The Linux Pocket Guide" from O'Reilly...

 

Remember that "[" is a command like any other, so it is followed by individual arguments separated by whitespace.

 

Ensure that there is white space around both side of "[" and "]", like this:

 

if [ "$COMMAND" = "1" ]; then

 

That's your problem, or I'm a dutchman.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...