how to combine two scripts into one


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to combine two scripts into one
# 1  
Old 05-27-2010
how to combine two scripts into one

I need to combine two scripts, into one script. One is to delete something, the to recreate. Here is the scripts:
Code:
BIN=/usr/lbin
LINE='device for 0101a01: lpd://172.25.41.111:515'
while read LINE
do
prt=`echo $LINE | awk '{print $3 }'| cut -c 1-7`
echo $prt
#drv=`echo $LINE | awk -F":" '{print $2}'`
#echo $drv
#IP=`echo $LINE | awk -F"/" '{print $3}' | cut -d":" -f1`
#echo $IP
#port=`echo $LINE | awk -F":" '{print $4}'`
#echo $port
  if [ "${prt}" = "0117bd1" ]; then
     echo "\n\t Deleting printer $prt  !!!"
     $BIN/lpadmin -x ${prt} 2> /dev/null
  fi
done < diffs.txt

recreate statement:
Code:
#!/bin/ksh -xv
BIN=/usr/lbin
LINE='device for 0101a01: lpd://172.25.41.111:515'
while read LINE
do
prt=`echo $LINE | awk '{print $3 }' | cut -c 1-7`
echo $prt
drv=`echo $LINE | awk -F":" '{print $2}'`
echo $drv
IP=`echo $LINE | awk -F"/" '{print $3}' | cut -d":" -f1`
echo $IP
port=`echo $LINE | awk -F":" '{print $4}'`
echo $port
  if [ "${prt}" = "0117bd1" ]; then
        echo " Adding printer $prt !!!!"
        $BIN/lpadmin -p ${prt} -E -v ${drv}://${IP}:${port}
        echo " Printer $prt:$drv:$IP:$port added!!!!"
  fi
done< diffs.txt

The variables are the same...the text file has three printers that need to be deleted and recreated...
# 2  
Old 05-27-2010
Consolidate your variable declarations and then either wrap the differing behaviors into separate sections of a case..esac statement, or wrap them into individual function() structures. Either can/should be approached based on $1 value of delete/recreate to the script itself.
# 3  
Old 05-27-2010
Code:
BIN=/usr/lbin

while read filler filler prt drv
do

  prt=${prt%%:*}
  IP=${drv##*/}
  IP=${IP%%:*}
  port=${drv##*:}

  echo "prt=[$prt] IP=[$IP] port=[$port]"

  if [ "${prt}" = "0117bd1" ]; then
     echo "\n\t Deleting printer $prt  !!!"
     $BIN/lpadmin -x ${prt} 2> /dev/null
  fi

  if [ "${prt}" = "0117bd1" ]; then
        echo " Adding printer $prt !!!!"
        $BIN/lpadmin -p ${prt} -E -v ${drv}://${IP}:${port}
        echo " Printer $prt:$drv:$IP:$port added!!!!"
  fi

done <diffs.txt

Jean-Pierre.
# 4  
Old 05-28-2010
Will give it a shot.thanks

---------- Post updated 05-28-10 at 09:51 ---------- Previous update was 05-27-10 at 15:51 ----------

Gave it a bash, and it worked fine for deleting 1 printer at a time. But if i need to delete printers in my text file can i do the following:
Code:
#Delete or Create Printer
   if [ -z "${prt}" ]; then
         echo "\n\t Deleting printer ${prt} !!!"
         $BIN/lpadmin -x ${prt} 2> /dev/null
   fi
   if [ -z "${prt}" ];then
         echo "\n\t Adding printers $prt !!!!"
         $BIN/lpadmin -p ${prt} -E -v ${drv}://${IP}:${port}
         echo "\n\t Printer $prt:$drv:$IP:$port created"
   fi
done < diffs.txt

using the -z or what other alternatives do i have?????Smilie
# 5  
Old 05-28-2010
You can do something like that (not tested):
Code:
BIN=/usr/lbin

while read filler filler prt drv
do

  prt=${prt%%:*}
  IP=${drv##*/}
  IP=${IP%%:*}
  port=${drv##*:}

  echo "prt=[$prt] IP=[$IP] port=[$port]"

  [ -z "${prt}" ] && continue

  echo "\n\t Deleting printer $prt  !!!"
  if ! $BIN/lpadmin -x ${prt} 2> /dev/null
  then
      echo "Failed to delete printer!"
      continue
  fi
  echo " Printer ${prt} deleted."

  echo " Adding printer $prt !!!!"
  if ! $BIN/lpadmin -p ${prt} -E -v ${drv}://${IP}:${port}
  then
      echo "Failed to add printer!"
      continue
  fi
  echo " Printer $prt:$drv:$IP:$port added!!!!"

done <diffs.txt
~

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Combine awk scripts

Hi, Below command is working as expected, but would like to know how to club the two AWK scripts in the command into one echo -e "MMS000101S0203430A|20180412E|\nMMB0001INVESTMENT||107-86193-01-03|\nMMB0001FUND||107-86193-04-01|\nMMC9991 " | awk -F'|' -v OFS=, '/^MMC9991/{print r"|"s,t; next}... (3 Replies)
Discussion started by: JSKOBS
3 Replies

2. Shell Programming and Scripting

Calling multiple scripts from another scripts

Dear all, I am working on script which call other shell scripts in a loop but problem is from second script am not able to come out. Here is the snippet:- #!/bin/bash HSFILE=/root/Test/Components.txt LOGFile=/opt/domain/AdminDomain/application/logs... (3 Replies)
Discussion started by: sharsour
3 Replies

3. Shell Programming and Scripting

KSH - How to call different scripts from master scripts based on a column in an Oracle table

Dear Members, I have a table REQUESTS in Oracle which has an attribute REQUEST_ACTION. The entries in REQUEST_ACTION are like, ME, MD, ND, NE etc. I would like to create a script which will will call other scripts based on the request action. Can we directly read from the REQUEST_ACTION... (2 Replies)
Discussion started by: Yoodit
2 Replies

4. Shell Programming and Scripting

Changing the Bash Scripts to Bourne Scripts:URGENT

Hi, I have to write a program to compute the checksums of files ./script.sh I wrote the program using bash and it took me forever since I am a beginner but it works very well. I'm getting so close to the deadline and I realised today that actually I have to use normal Bourne shell... (3 Replies)
Discussion started by: pgarg1989
3 Replies

5. Shell Programming and Scripting

Running scripts within scripts from cron

Hi all, I have set up a cron job which calls another shell script shell script which in turn calls a Java process. The cron tab looks so. 0,30 7-18 * * 1-5 /u01/home/weblogic/brp/bin/checkstatus.sh >> /u01/home/weblogic/logs/checkstatus.log The checkstatus.sh scripts looks like this. ... (4 Replies)
Discussion started by: sirbrian
4 Replies

6. Shell Programming and Scripting

Help with Script using rsh and scripts within scripts

Hi, I've written a script that runs on a Database server. It has to shutdown the Application server, do an Oracle Dump and then restart the Application server. Its been a long time since I wrote any shells scripts. Can you tell me if the scripts that I execute within my script will be executed... (3 Replies)
Discussion started by: brockwile1
3 Replies

7. Shell Programming and Scripting

help with a script that will combine two separate scripts for solaris and aix

Hello experts, I have separate scripts (KSH) each for Solaris and AIX to install core applications (e.g. BigBrother). I also have a script called Installer that gives a menu list to select a particular application to install from a bunch of applications. Now I am trying to combine separate... (7 Replies)
Discussion started by: solaix14
7 Replies

8. UNIX for Dummies Questions & Answers

Profile scripts versus rc scripts....

what is the difference between login and profile scripts versus the rc scripts? (1 Reply)
Discussion started by: rookie22
1 Replies

9. Shell Programming and Scripting

i want to combine two awk scripts which is having same loop and filelist

hi, to all give me some idea below scripts file=`cat filelist` for filename in $file do awk '{ if ($0 ~ /system/ && flag == 0) { flag=1; print $0; } else if ($0 ~ /system/ && flag == 1) { printf("%s SYSLAY %s %s %s\n",$1,$3, $4, $5); } else print $0; }' $filename >... (6 Replies)
Discussion started by: LAKSHMI NARAYAN
6 Replies
Login or Register to Ask a Question