Sponsored Content
Top Forums Shell Programming and Scripting Calling external function in a shell Post 302283076 by cfajohnson on Monday 2nd of February 2009 01:38:01 PM
Old 02-02-2009
Quote:
Originally Posted by acevallo
PLEASE GUYS HELP ME, I am new in this forum, if I have mistakes with rules I hope to do better my posts, je je je j Smilie

Please put code inside [code] tags.
Quote:
-----------------------
Code:
 
get_one_day_before_specified_date()
{
    #get the command line input(date month & year)
    day=$1
    month=$2
    year=$3
    # if it is the first day of the month
    if [ $day -eq 01 ]
    then
        # if it is the first month of the year
        if [ $month -eq 01 ]
        then
            # make the month as 12
            month=12
            # deduct the year by one
            year=`expr $year - 1`


There is no need to use an external command; the shell has integer arithmetic built in:

Code:
year=$(( $year - 1 ))

Quote:
Code:
            if [ $year -lt 10 ]
                then
                    year=0$year
            fi
        else
            # deduct the month by one
            month=`expr $month - 1`


See above.
Quote:
Code:
     fi
     if [ $month -lt 10 ]
        then
        month=0$month
     fi
        # use cal command, discard blank lines,
        # take last field of last line,
        # first awk  command is used to get the
        # last useful line of the calendar cmd,
        # second awk command is used to get the
        # last field of this last useful line,
        # NF is no. of fields,
        # $NF is value of last field
    day=`cal $month $year |
      awk 'NF != 0{ last = $0 }; END{ print last }' |
      awk '{ print $NF }'`


It is unwise to use a command whose output is not specified and may differ among implementations.

If you really want to use cal, you don't need two calls to awk:

Code:
: $( cal "$month" "$year" )
day=$_

Quote:
Code:
    else
        # deduct the day by one
        day=`expr $day - 1`


Ditto.
Quote:
Code:
    fi
    if [ $day -lt 10 ]
    then
      day=0$day
    fi
    fecha_before=$day'-'$month'-'$year


You don't need those quotes:

Code:
fecha_before=$day-$month-$year

Quote:
Code:
    return $fecha_before


You can only return integers (255 or less).
Quote:
Code:
}

-----------------------

To get yesterday's day, I use a function, _yesterday, which uses functions from my book Shell Scripting Recipes: A Problem-Solution Approach:

Code:
_yesterday () 
{ 
    _date2julian "$1" || return 2;
    _julian2date $(( $_DATE2JULIAN - 1 )) || return 2;
    _YESTERDAY=$_JULIAN2DATE
}

The chapter that deals with dates is online at The Dating Game.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

calling a c function from shell

Is it possible to call a C function from within a shell script. This C function is part of an API. What do I need to make it work from my shell script. Anybody please help. (4 Replies)
Discussion started by: seshagiri
4 Replies

2. UNIX for Dummies Questions & Answers

calling one function from another shell script

i have a function defined in one ksh (ksh 1) i want to use that function in another ksh (ksh 2) i am using . $<directoryname>/<ksh name> i am calling the function defined in ksh 1 in ksh 2 i want the returnstatus from the above operation but it is not executing the function what i... (1 Reply)
Discussion started by: trichyselva
1 Replies

3. Shell Programming and Scripting

Calling a C-function froma shell script

Hi, I have searched the forum for the query, But i didnt find an exact answer. I have a script(1.sh) and a c program(sample.c) sample.c contains many function definitions.( run(), find(), add() etc). I want to call functions in sample.c from 1.sh and use the return value in 1.sh... (3 Replies)
Discussion started by: jisha
3 Replies

4. Shell Programming and Scripting

calling a function in Shell script troubleshooting

Some Code After Some code part is executed the control doesnt go to rvin_doxx_scrt.. and the script exits rvin_doxx_scrt() { Some Code } if (som code) ... (4 Replies)
Discussion started by: ultimatix
4 Replies

5. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

6. Homework & Coursework Questions

Shell script calling Perl function, sort and find data, write to new files

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I must write a shell script that calls two external Perl functions--one of which sorts the data in a file, and... (6 Replies)
Discussion started by: kowit010
6 Replies

7. Shell Programming and Scripting

Is it possible make the shell read functions 1 by 1 and calling an other function?

Greetings, I m wondering if it's possible do do the following : I have a simple function called "FindMoveDelete" which does the following : FindMoveDelete() { find . -iname "$FILENAME*.ext" -exec mv {} "$PATH/$VAR" \; && find . -maxdepth 1 -type d -iname "$FILENAME*" -exec rm -rf {}... (6 Replies)
Discussion started by: Sekullos
6 Replies

8. Shell Programming and Scripting

Calling a function in cpp file inside shell script

Hi I need to call a function written in a cpp file with arguments inside the shell script..Can anyone help me how to do this:( (1 Reply)
Discussion started by: rkrish
1 Replies

9. Shell Programming and Scripting

Calling Pl/sql function in shell script to modify csv

I need to 1.Open a csv 2.Process the csv i.e. Modify 2 column in the csv. To modify the column the value needs to be passed to a pl/sql function and the return value should be updated For eg: If column 2 E,then E will be passed in database function which will return Employee. 3. Write a... (5 Replies)
Discussion started by: Chinky23
5 Replies

10. Shell Programming and Scripting

Multithreading - Calling user function with xargs in korn shell

I know there are other ways of accomplishing the below task, but the purpose of this thread is to understand the below code. I wanted to use xargs with user defined function in korn shell. Am aware, that I could write custom function into a script and place it in FPATH and then call it in xargs,... (2 Replies)
Discussion started by: luhah
2 Replies
All times are GMT -4. The time now is 11:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy