Solved

calling functions from shell script

Posted on 2010-08-15
8
509 Views
Last Modified: 2013-12-26
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

0
Comment
Question by:Micheal_Male
  • 4
  • 4
8 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 33441577
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
 

Author Comment

by:Micheal_Male
ID: 33441599
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
 
LVL 48

Expert Comment

by:Tintin
ID: 33441712
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
 

Author Comment

by:Micheal_Male
ID: 33441733
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 48

Accepted Solution

by:
Tintin earned 250 total points
ID: 33441826
Yes, you can pass an argument.

With no error checking, you can simply do

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

function two
{
 ..
}

$1
0
 

Author Comment

by:Micheal_Male
ID: 33442640
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
 

Author Comment

by:Micheal_Male
ID: 33442662
Thank you i think i got it what you are trying to say.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 33443532
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now