shell script for if logiic or for logic

please provide  a shell script for following description


please provide  a shell script for following description

1) i have 5 ear files called /usr/local/web/web.ear, /usr/local/arc/arc.ear, /usr/local/des/des.ear, /usr/local/pow/pow.ear and /usr/local/prp/prp.ear on serverA(linux)
2) some times i will have to copy (for copying to different server i use scp) just web.ear and des.ear to serverB(linux) and someother times i will have to copy prp.ear and someother ear, so i will be copying all 5 or just 1 or 2 or 3 or 4.

i want a simple shell script which will have if logic or for logic, then i can pass the ear name as the command prompt and the shell script wil copy only that ear to serverB like this ./shellscriptname.sh web.ear arc.ear
wasmanIT AnalystAsked:
Who is Participating?
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.

xtermCommented:
This should do what you want.  It also checks to make sure you supplied at least one argument to the script.
#!/bin/sh

if [ -z $1 ]; then
        echo "Usage: `basename $0` [ear1] [ear2] ... [earX]"
        exit 1
fi

ARGCOUNT=$#
COUNT=1
BASE="/usr/local"

while [ $COUNT -le $ARGCOUNT ];
do
        PREF=`echo $* | tr " " "," | cut -d "," -f$COUNT | cut -d. -f1`
        EARPATH="$BASE/$PREF/$PREF.ear"
        echo "Copying $EARPATH to server1"
        scp $EARPATH server1
        COUNT=`expr $COUNT + 1`
done

Open in new window

0
simon3270Commented:
You need a colon after "server1", otherwise it will just create a local file on serverA called server1.  Using the original question's terminology:

#!/bin/bash

if [ $# -lt 1 ]
then
  echo "Usage: $(basename $0) ear1.ear [ear2.ear [ear3.ear [... ] ] ]
  exit 1
fi

for PREF in $*
do
  EARFILE=/usr/local/$PREF
  if [ -f $EARFILE ]
  then
    scp $EARFILE serverB:/path/on/server/B
    rc=$?
    echo File $EARFILE copied to serverB, return code $rc
  else
    echo File $EARFILE not found
    exit 2
  fi
done

Open in new window

0
xtermCommented:
my scp line was just an example since the question didn't state a path to copy to on the remote server - it is assumed the author will simply substitute in the path and server name that he has been using.

Your script would not find the source files however because you're looking for /usr/local/<filename> but the author states that the files are in /usr/local/<filename without extension>/<filename>.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

simon3270Commented:
Even if there is no path, you still need the colon to tell scp that this is a remote file.

The example call supplied was "shellscriptname.sh web.ear arc.ear", so the .ear is in the parameters.
0
xtermCommented:
The example call supplied was "shellscriptname.sh web.ear arc.ear", so the .ear is in the parameters.

Yes, but your script would be looking for /usr/local/arc.ear and not find it because its in /usr/local/arc/arc.ear - read the question again.

And I know the scp command was incomplete and just an example (obviously server1 isn't a real hostname either) - the assumption is the author already knows how to scp, and that isn't the point of this question, so I'm not clear on the reason to belabor the point.
0
simon3270Commented:
Yes, you are right, I had missed the subdirectory - I just lost it in the unusual way the parameters were being parsed.

Just use this instead:
EARFILE=/usr/local/${PREF%.ear}/$PREF

Open in new window


As for scp, the author clearly already understands scp, but other readers of this question may not.
0
wasmanIT AnalystAuthor Commented:
sorry if i confuse you , below are the individual scp commmands i have for copying the ear files,
i like to execute the shell script like this ./script.sh webls webbt , so please provide a shell script which is simple and i can modify it.


R=R22.3.2
# 1) below script will copy webls.ear to server2:/usr/local/webls folder
ssh xx22197@server2 "rm -r /var/tmp/$R"
ssh xx22197@server2 "mkdir -p /var/tmp/$R/webls "
scp /usr/local/webls/* xx22197@server2:/var/tmp/$R/webls/ ;
ssh xx22197@server2 "sudo -u webadmin cp /var/tmp/$R/webls/* /usr/local/webls/"

# 2) below script will copy webls.ear to server2:/usr/local/webbt folder
ssh xx22197@server2 " mkdir -p /var/tmp/$R/webbt"
scp /usr/local/webbt/* xx22197@server2:/var/tmp/$R/webbt/ ;
ssh xx22197@server2 "sudo -u webadmin cp /var/tmp/$R/webbt/* /usr/local/webbt/"

# 3) below script will copy webls.ear to server2:/usr/local/webps folder
ssh xx22197@server2 " mkdir -p /var/tmp/$R/webps"
scp /usr/local/webps/* xx22197@server2:/var/tmp/$R/webps/ ;
ssh xx22197@server2 "sudo -u webadmin cp /var/tmp/$R/webps/* /usr/local/webps/"

# 4)  below script will copy webls.ear to server2:/usr/local/websb folder
ssh xx22197@server2 " mkdir -p /var/tmp/$R/websb"
scp /usr/local/websb/* xx22197@server2:/var/tmp/$R/websb/ ;
ssh xx22197@server2 "sudo -u webadmin cp /var/tmp/$R/websb/* /usr/local/websb/"

# 5) below script will copy webls.ear to server2:/usr/local/webws folder
ssh xx22197@server2 " mkdir -p /var/tmp/$R/webws"
scp /usr/local/webws/* xx22197@server2:/var/tmp/$R/webws/ ;
ssh xx22197@server2 "sudo -u webadmin cp /var/tmp/$R/webws/* /usr/local/webws/"

# 6) below script will copy web.zip to server1:/usr/local/webls and server2:/usr/local/webls folder
ssh xx22197@server2 " mkdir -p /var/tmp/$R/webcontent"
ssh xx22197@server4 " scp /usr/local/webls/web.zip xx22197@server1:/var/tmp/$R/webcontent  ; scp /usr/local/webls/web.zip xx22197@server2:/var/tmp/$R/webcontent "
sudo -u webadmin cp /var/tmp/$R/web.zip /app/wam/dep/prod/webls/ ; ssh xx22197@server2 " sudo -u webadmin cp /var/tmp/$R/web.zip /app/wam/dep/prodf/webls/ "


please let me know if you have question
0
xtermCommented:
The script below will work for scenarios 1-5.  Scenario 6 is a bit outside the scope of what you asked for, but hopefully you can see how the script works and modify it appropriately for your needs.
#!/bin/sh

if [ -z $1 ]; then
        echo "Usage: `basename $0` [webls|webbt|webps|websb|webws]"
        exit 1
fi

ARGCOUNT=$#
COUNT=1
BASE="/usr/local"

while [ $COUNT -le $ARGCOUNT ];
do
        PREF=`echo $* | tr " " "," | cut -d "," -f$COUNT | cut -d. -f1`
        ssh xx22197@server2 "rm -r /var/tmp/$PREF"
        ssh xx22197@server2 "mkdir -p /var/tmp/$PREF"
        scp /usr/local/$PREF/* xx22197@server2:/var/tmp/$PREF
        ssh xx22197@server2 "sudo -u webadmin cp /var/tmp/$PREF/* /usr/local/$PREF"
        COUNT=`expr $COUNT + 1`
done

Open in new window

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
wasmanIT AnalystAuthor Commented:
hi xterm,
one thing i missed, please note the destination folder structure is different then the source, below is the correct ssh/scp commands

R=R22.3.2
# 1) below script will copy webls.ear to server2:/usr/local/webls folder
ssh xx22197@server2 "rm -r /var/tmp/$R"
ssh xx22197@server2 "mkdir -p /var/tmp/$R/webls "
scp /usr/local/webls/* xx22197@server2:/var/tmp/$R/webls/ ;
ssh xx22197@server2 "sudo -u webadmin cp /var/tmp/$R/webls/* /usr/local/prodg/webls/"

# 2) below script will copy webls.ear to server2:/usr/local/webbt folder
ssh xx22197@server2 " mkdir -p /var/tmp/$R/webbt"
scp /usr/local/webbt/* xx22197@server2:/var/tmp/$R/webbt/ ;
ssh xx22197@server2 "sudo -u webadmin cp /var/tmp/$R/webbt/* /usr/local/prodg/webbt/"

# 3) below script will copy webls.ear to server2:/usr/local/webps folder
ssh xx22197@server2 " mkdir -p /var/tmp/$R/webps"
scp /usr/local/webps/* xx22197@server2:/var/tmp/$R/webps/ ;
ssh xx22197@server2 "sudo -u webadmin cp /var/tmp/$R/webps/* /usr/local/prodg/webps/"

# 4)  below script will copy webls.ear to server2:/usr/local/websb folder
ssh xx22197@server2 " mkdir -p /var/tmp/$R/websb"
scp /usr/local/websb/* xx22197@server2:/var/tmp/$R/websb/ ;
ssh xx22197@server2 "sudo -u webadmin cp /var/tmp/$R/websb/* /usr/local/prodg/websb/"

# 5) below script will copy webls.ear to server2:/usr/local/webws folder
ssh xx22197@server2 " mkdir -p /var/tmp/$R/webws"
scp /usr/local/webws/* xx22197@server2:/var/tmp/$R/webws/ ;
ssh xx22197@server2 "sudo -u webadmin cp /var/tmp/$R/webws/* /usr/local/prodg/webws/"


sorry for inconvience , if you want i can close this ticket and create an new one
0
xtermCommented:
Sorry, I missed that you'd actually defined $R at the top, here you go:
#!/bin/sh

if [ -z $1 ]; then
        echo "Usage: `basename $0` [webls|webbt|webps|websb|webws]"
        exit 1
fi

ARGCOUNT=$#
COUNT=1
BASE="/usr/local"

while [ $COUNT -le $ARGCOUNT ];
do
        PREF=`echo $* | tr " " "," | cut -d "," -f$COUNT | cut -d. -f1`
        echo ssh xx22197@server2 "rm -r /var/tmp/R22.3.2/$PREF"
        echo ssh xx22197@server2 "mkdir -p /var/tmp/R22.3.2/$PREF"
        echo scp /usr/local/$PREF/* xx22197@server2:/var/tmp/R22.3.2/$PREF
        echo ssh xx22197@server2 "sudo -u webadmin cp /var/tmp/R22.3.2/$PREF/* /usr/local/$PREF/"
        COUNT=`expr $COUNT + 1`
done

Open in new window

0
wasmanIT AnalystAuthor Commented:
nope...the R22.3.2 is good....did you noticed the destination folder structure it is /usr/local/prodg/webls and so on
0
xtermCommented:
No, sorry, I did not.  Just change the last part of line 19 to /usr/local/prodg/$PREF instead of /usr/local/$PREF

This is difficult for us, because you've changed your requirements several times throughout the course of this question.
0
wasmanIT AnalystAuthor Commented:
perfect this one really solved my question 100% . Thank you very much xterm.
0
xtermCommented:
Anytime!  Hopefully it saves you lots of time & will help you with future shell scripts.
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.