calling functions from shell script

I have a script in which i need to transfer files from a - b destination. First what is the best way to transfer huge files ? scp or ftp or using a for loop and transferring it through scp one by one. Secondly i want to create functions which when called from a shell script should execute that part of the function only. Any input on how to do it ?. Below is the code now how can i run my script through command line to execute only function one() ? and later on function two() ?


#!/bin/sh

SOURCE_ONE="/home/user/a"
DEST="/home/user/x"
SOURCE_TWO="/home/user/b"
ERROR=0

function one() {
scp $SOURCE/*.csv mike@server:$DEST
if [ $? != 0 ]; then
ERROR = 1
fi
}

functio two() {
scp $SOURCE_TWO/*.csv mike@server:$DEST
}

if [ $ERROR != 0 ]; then
sendmail ......
fi

Open in new window

Micheal_MaleAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TintinCommented:
I'd recommend using rsync if you are going to be regularly transfering files.  The advantage of rsync is that if you connection drops out, it will transfer the remaining parts of the file.

If you want to use functions from multiple locations, separate them out.

For example, create a file /usr/local/include/functions.sh

with

one()
{
  echo function one
}

two()
{
  echo function two
}


Then in your shell scripts, you can access them with

#!/bin/sh
. /usr/local/include/functions.sh

one
0
Micheal_MaleAuthor Commented:
Yes but I am trying to avoid creating seperate scripts. On Saturday I want the script to run function one and then on Sunday I want the script to execute function 2 and so on.   I know I can create seperate scripts but wanted to write one script which can achieved multiple task like passing an argument through command line so it invokes that part of specific scrippt only.
0
TintinCommented:
In that case, you can do something like

#!/bin/bash
DAY=$(date +%a)

function Monday
{
  ..
}

function Tuesday
{

}

case $DAY in
Mon)  Monday
           ;;
Tue)  Tuesday
           ;;
esac

0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Micheal_MaleAuthor Commented:
So therevis no way to pass an argument to my script through command line to invoke that function only?  My script name is test.sh so can't I do this. ./test.sh one
0
TintinCommented:
Yes, you can pass an argument.

With no error checking, you can simply do

#!/bin/bash
function one
{
  ..
}

function two
{
 ..
}

$1
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Micheal_MaleAuthor Commented:
Tintin. Thank you for your response but i still don't understand how $1 will call my function one () ?. I tried this but the function never gets invoked.

./test.sh $1

Nothing happens. I want to pass an argument through a command line when running the script so it executes only function one() for argument one and function two() for argument two.

./test.sh $one (Should call function one() only in the script)
./test.sh $two (Should call function two() only in the script)

Sorry for the repetation or misunderstanding.
#!/bin/sh

SOURCE_ONE="/home/user/a"
DEST="/home/user/b"
SOURCE_TWO="/home/user/b"
ERROR=0

function one() {
echo "insode function one"
scp $SOURCE/*.csv mike@server:$DEST
if [ $? != 0 ]; then
ERROR = 1
fi
}

function two() {
echo "inside function two"
scp $SOURCE_TWO/*.csv mike@server:$DEST
}

Open in new window

0
Micheal_MaleAuthor Commented:
Thank you i think i got it what you are trying to say.
0
TintinCommented:
Just in case you didn't fully understand, $1 is inside the script and will contain the value of first argument passed to the script.

So if you type

./test.sh one

Then $1 in test.sh will be set to 'one'.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.