LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-28-2011, 03:55 PM   #1
wills
LQ Newbie
 
Registered: Jun 2008
Posts: 28

Rep: Reputation: 0
Bash read method 'defaults'


Hi,

I have a script that will ask the operator to specify whether the script should fetch the bits via FTP or SCP:

Code:
# Specify transfer method
echo "[setup]: Please specify file transfer method [ftp] or [scp]:"
 read method

if [ $method == "ftp" ]
	then
		cd $B_DIRECTORY && $LFTP -u $USER,$SECRET $FTP_SERVER -e "pget $PKGS ; exit"
	else
		$SCP "$USER"@"$SSH_SERVER":/"$R_DIRECTORY/$PKGS" ./"$B_DIRECTORY"
	exit
fi
What I'm interested in is to figure out a way to pass along the switch either from the command (e.g. script.sh --ftp) and have the script read that or resort to using the read method by having operator enter it. I'm doing this for the operator that wants to throw in the switches to be more efficient

Any suggestions?
 
Old 03-28-2011, 04:10 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Sure, you can use $1 for a command line parameter, as such:
Code:
#!/bin/bash
ftp1="--ftp"
scp1="--scp"
if [ "$1" = "$ftp1" ]
then
cd $B_DIRECTORY && $LFTP -u $USER,$SECRET $FTP_SERVER -e "pget $PKGS ; exit"
elif [ "$1" = "$scp1" ]
then
$SCP "$USER"@"$SSH_SERVER":/"$R_DIRECTORY/$PKGS" ./"$B_DIRECTORY"
else
echo "Please use either --ftp or --scp"
fi
I don't know if this code will run right away, might need to be tweaked up.

Cheers,

Josh
 
Old 03-28-2011, 08:13 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Are you passing any other data on the command line currently? If you are you would again need to tweak, but you will get the idea:
Code:
#!/bin/bash

TYPE=''
EXIT=0

if (( $# == 1 ))
then
    TYPE=$1
else
    echo "[setup]: Please specify file transfer method [ftp] or [scp]:"
    read TYPE
fi

case $TYPE in
    --ftp) cd $B_DIRECTORY && $LFTP -u $USER,$SECRET $FTP_SERVER -e "pget $PKGS ; exit";;
    --scp) $SCP "$USER"@"$SSH_SERVER":/"$R_DIRECTORY/$PKGS" ./"$B_DIRECTORY"
    *) echo "Usage: $0 [--ftp|--scp]"; EXIT=1;;
esac

exit $EXIT
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash + Recursive Method SuchANewb Linux - Newbie 1 11-04-2010 08:51 AM
[SOLVED] [Perl] OOP Accessor Method for Array Attributes and Append Method brianmcgee Programming 1 10-18-2010 11:05 AM
bash ctrl+c tarp and bash read with timeout !?! bull81 Programming 10 01-25-2010 09:56 AM
BASH script emulation of HTML POST method ? bjh Programming 2 02-10-2008 05:30 PM
bash defaults marioV Linux - Software 4 11-16-2005 10:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration