Sponsored Content
Full Discussion: Portable shell script advise
Top Forums Shell Programming and Scripting Portable shell script advise Post 302972008 by MadeInGermany on Thursday 28th of April 2016 09:21:43 AM
Old 04-28-2016
Here is one that should be safe, efficient, and portable even to older Solaris and HP-UX.

Code:
#!/bin/sh

# we want to find programs here, not in an inherited PATH
PATH=/bin:/usr/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin
export PATH

dest=servername
inst=INST2
month=`date +%b%Y`
logdir=/path/to/log/dir
logfile=${logdir}/xfer_${1}_${month}.log
fmask="iS*"

# open once with descriptor 4
exec 4>"$logfile"
find 
#exec 4>>"$logfile"
# writing to descriptor 4 will append to the open file

case $1 in

us)
    filedir=/path/to/us/dir
    destdir=/path/${inst}/path/us
;;
eu)
    filedir=/path/to/eu/dir
    destdir=/path/${inst}/path/eu
;;
*)
    echo "unexpected argument: $1"
    exit 1
;;

esac

# --------------------------------------------------
# functions
# --------------------------------------------------

usage () {

    echo "usage info"

}

header () {

    divider="=============================="
    divider=$divider$divider$divider

    printf "\n$divider\n" >&4
    echo "$1 -- `date`" >&4
    printf "$divider\n" >&4

}

# --------------------------------------------------
# main
# --------------------------------------------------

if [ $# -ne 1 ]; then

    usage
    exit 1

fi

# make sure we can go there
if cd $filedir
then

    files=`ls -- $fmask 2>/dev/null`
    header "$1 files to send"

    if [ -z "$files" ]; then

        printf "\n No $1 files to transfer" >&4

    else

        # a lazy printf "$files\n" or echo "$files" is at risk
        printf "%s\n" "$files" >&4

        header "Sending $1 files"
        # a trailing / makes sure it is a directory
        scp -- $fmask ${dest}:${destdir}/ >&4 2>&1

        if [ $? -ne 0 ]; then

            printf "\n  There are no files to transfer..." >&4
            header "Complete"

        else

            header "$1 files successfully sent"

            header "Moving files and purge 30+ days"
            mv -f -- $fmask ${filedir}/archive/ >&4 2>&1
# + collects arguments and calls the program very few times -> faster; no -- needed in find -exec prog {}
# HP-UX does not have -ls
            find ${filedir}/archive/ -type f -mtime +30 -exec ls -l {} + -exec rm -f {} + >&4 2>&1
            header "Finished moving files.."

        fi

    fi

fi

# explicit close of logfile not needed before an exit or end of file
#exec 4>&-

exit


Last edited by MadeInGermany; 04-28-2016 at 03:05 PM.. Reason: no -- needed in find -exec prog {}
 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Can somebody advise any free Linux sever for shell programming?

Hi, everybody. I just wonder whether there are a couple of free Linux servers running as terminals where people can practice Unix Shell Programming? I'd like to set up one myself but unfortunatly can't do it. I can't switch to Linux now coz I run a couple of servers on my machine. Cygwin is... (3 Replies)
Discussion started by: belgampaul
3 Replies

2. Windows & DOS: Issues & Discussions

Portable GUI shell scripting?

I have some scripts that run in Windows, and the ability to put a GUI layer over them is kind of appealing. So when I stumbled over http://wizapp.sourceforge.net/ I thought that was rather cute. (Reading the "advanced batch scripting" art of the manual impresses upon me that no sane person... (2 Replies)
Discussion started by: luke
2 Replies

3. Shell Programming and Scripting

mail program on shell script didn't work, please advise.

Hi, everyone: I post a new thread because previous post may sink and I hope the new one can be caught by your eyes. I created a shell script and the script works fine. However, the mail program part on script didn't send email to my email box and it also didn't provide any traceable... (7 Replies)
Discussion started by: duke0001
7 Replies

4. Shell Programming and Scripting

Query regarding portable script

Dear Experts, I want to write a script which has to work on Solaris & Linux sytems. The problem which i am facing is, there are commands whose options are different on both OS's. For example ping. On Solaris i have to write: ping $host 1 to check if the host is alive On Linux i... (4 Replies)
Discussion started by: dhiraj4mann
4 Replies

5. Shell Programming and Scripting

advise some script to make the changes..!!

Hi Folks, got the solution..!! (5 Replies)
Discussion started by: tuntun
5 Replies

6. Shell Programming and Scripting

Solaris script using awk giving errors - please advise

I'm using solaris 10 Scenario as follows I have a logfile with 2 columns: column 1 = source directory + filename column 2 = destination directory + filename Using cron, my script polls for new files and adds them to the logfile ($ELOG) as described above. Using sed, the distination... (2 Replies)
Discussion started by: davidra
2 Replies

7. Shell Programming and Scripting

Portable Shell Script - Determine Which Version of Binary is Installed?

I currently have a shell script that utilizes the "Date" binary - this application is slightly different on OS X (BSD General Commmand) and Linux systems (gnu date). In particular, the version on OS X requires the following to get a date 14 days in the future "date -v+14d -u +%Y-%m-%d" where gnu... (1 Reply)
Discussion started by: colinjohnson
1 Replies

8. Shell Programming and Scripting

Optimizing the Shell Script [Expert Advise Needed]

I have prepared a shell script to find the duplicates based on the part of filename and retain latest. #!/bin/bash if ; then mkdir -p dup fi NOW=$(date +"%F-%H:%M:%S") LOGFILE="purge_duplicate_log-$NOW.log" LOGTIME=`date "+%Y-%m-%d %H:%M:%S"` echo... (6 Replies)
Discussion started by: gold2k8
6 Replies

9. Shell Programming and Scripting

Tip: template for a safe and portable script

In an attempt to finally end this article I start this new thread. Here is a template for a safe and portable script. #!/bin/bash # /bin/bash exists on most still supported Unixes # # tr and date behave better with if ; then export LC_ALL=C; else export LANG=C; fi # # Unix optional packages... (2 Replies)
Discussion started by: MadeInGermany
2 Replies
All times are GMT -4. The time now is 04:42 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy