Select variable within a if statement

i want to select a variable created and use it in a if statement, but not getting the desired results

LINE='device for 0101a01: lpd://172.25.41.111:515'
prt=`echo $LINE | awk '{print $3 }' | cut -c 1-7`
echo $prt

My if statement to select just what i want..

IFS=$":"
while read prt drv IP port
do
  if [ -z "$prt" = "0117bd1" ]; then
      echo " Adding printer $prt: !!!!"
done

Just try with

if [ "${prt}" == "0117bd1" ]

cheers,
Devaraj Takhellambam

I tried , but its not giving me just the printer name , because that is what i actually just need.
My variable if tested gives me just that.

+ cut -c 1-7
+ awk {print $3 }
prt=0101a01
+ echo 0101a01
0101a01

Giving me the full details instead of just the cut as per my variable

+ IFS=$:
+ 0< diffs.txt
+ read prt drv IP port
+ [ device for 0117bd1 == 0117bd1 ]

Any other suggestions...

try:

prt=$( echo $LINE | awk -F "[ :]" '{print $3'})

with:

if [ "${prt}" == "0117bd1" ]

use this if using ksh

if [ "${prt}" = "0117bd1" ]

Hi.

You could do it this way:

  1. Remove IFS=$:
  2. Change the read to
read blah blah prt drv IP port
  1. Change the if to
if [ "$prt" = "0117bd1:" ]

or

if [ "${prt%:}" = "0117bd1" ]

I tried all of these with no success...
herewith is output received with 1st option

+ 0< diffs.txt
+ read prt drv IP port
+ [ device = 0117bd1: ]
+ read prt drv IP port
+ [ device = 0117bd1: ]
+ read prt drv IP port
+ [ device = 0117bd1: ]

second option:

+ 0< diffs.txt
+ read prt drv IP port
+ [ device = 0117bd1 ]
+ read prt drv IP port
+ [ device = 0117bd1 ]
+ read prt drv IP port
+ [ device = 0117bd1 ]
+ read prt drv IP port

Hi.

Better if you show your script and not just the set -x output...

ok here is the script

BIN=/usr/lbin
LINE='device for 0101a01: lpd://172.25.41.111:515'
prt=`echo $LINE | awk '{print $3 }' | cut -c 1-7`
#prt=$( $LINE | awk -F "[ :]" '{print $3'})
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
#IFS=$":"
while read prt drv IP port
do
  if [ "${prt%:}" = "0117bd1" ]; then

        echo " Adding printer $prt !!!!"
       $BIN/lpadmin -p $prt -E -D $drv $IP $port

        echo " Printer $prt:$drv:$IP:$port added!!!!"
  fi

done< diffs.txt

what does you file diffs.txt has? Can you show us the content of this file.

This is the place where your prt is getting picked from.

here is the diffs file

more diffs.txt
device for 0117bd1: lpd://172.25.29.60:515
device for 2001bl7: socket://172.25.44.188:9100
device for 2201bl7: socket://172.25.11.170:9100
device for 5001bl1: lpd://172.25.11.203:515
device for 5001x01: lpd://172.25.12.160:515
device for 5001x02: lpd://172.25.12.160:515
BIN=/usr/lbin
while read LINE
do
prt=`echo $LINE | awk '{print $3 }' | cut -c 1-7`
drv=`echo $LINE | awk -F":" '{print $2}'`
IP=`echo $LINE | awk -F"/" '{print $3}' | cut -d":" -f1`
port=`echo $LINE | awk -F":" '{print $4}'`
  if [ "${prt}" = "0117bd1" ]; then
        echo " Adding printer $prt !!!!"
        $BIN/lpadmin -p $prt -E -D $drv $IP $port

        echo " Printer $prt:$drv:$IP:$port added!!!!"
  fi

done< diffs.txt

cheers,
Devaraj Takhellambam

thanks..this gave me exactly what i needed.:slight_smile: