Shell scirpt error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell scirpt error
# 1  
Old 12-02-2015
Shell scirpt error

I am not getting 1 for the failure scenario for my below code. any help would be greatly appreciated. If the input is invalid i should get exit. But in the below scenario i am not getting the message exit o.

Code:
ksh -x client_check.ksh sun
+ + hostname
hn=us
+ [[ us= us ]]
+ [ ! -d ]
client_check.ksh[18]: test: argument expected
+ exit 0


pikd>shell_scripts> cat clientcheck.ksh
#!/usr/bin/ksh
#clientcheck.ksh zxx
#set -x
hn=`hostname`
if [[ $hn = "us" ]]
then
  case $1 in
      ban)   client_home='/comps/banking'
             ;;
      man)     client_home='/comps/manufacturer'
             ;;
   esac
fi
if [ ! -d ${client_home} ]; then
   exit 1
else
   exit 0
fi

# 2  
Old 12-02-2015
Try changing:
Code:
#!/usr/bin/ksh
#clientcheck.ksh zxx
#set -x
hn=`hostname`
if [[ $hn = "us" ]]
then
  case $1 in
      ban)   client_home='/comps/banking'
             ;;
      man)     client_home='/comps/manufacturer'
             ;;
   esac
fi
if [ ! -d ${client_home} ]; then
   exit 1
else
   exit 0
fi

to:
Code:
#!/usr/bin/ksh
#clientcheck.ksh zxx
#set -x
hn=`hostname`
if [[ $hn = "us" ]]
then
  case "$1" in
      ban)   client_home='/comps/banking'
             ;;
      man)     client_home='/comps/manufacturer'
             ;;
      *)     exit 1;;
   esac
fi

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 12-02-2015
Hello Arun,

Could you please change if [[ $hn = "us" ]] to if [[ "$hn" == "us" ]] and let me know if this helps.
EDIT: Thank you Don, I think Don has given perfect solution, please try that solution.

Thanks,
R. Singh

Last edited by RavinderSingh13; 12-02-2015 at 04:30 AM.. Reason: Adding EDIT comment for Don's solution
This User Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 12-02-2015
Quote:
Originally Posted by RavinderSingh13
Hello Arun,

Could you please change if [[ $hn = "us" ]] to if [[ "$hn" == "us" ]] and let me know if this helps.

Thanks,
R. Singh
This isn't likely to help. (It will help if and only if hostname aborts or returns a host name containing whitespace characters.)

The problem shown in the trace is that the operand passed to the script (sun) is not a choice in the case statement. Therefore, the client_home variable is never set and the following test to determine if $client_home expands to the name of a directory gets a syntax error.

The change I suggested in post #2 in this thread short-circuits the problem by exiting immediately if client_home will not be set by the given operand.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 12-02-2015
thanks for your help ravi/don.

I am not sure why the $1 variable is not assigned to the below code.

Code:
  case $1 in
      ban)   client_home='/comps/banking'
             ;;
      man)     client_home='/comps/manfucture'
             ;;
       *)     client_home='/comps/$1'
             ;;

ksh -x client_check.ksh jkknk
+ client_home=/comps/$1

# 6  
Old 12-02-2015
That's because of the single quotes, which prohibit shell's variable expansion. Use double quotes instead.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 12-02-2015
thanks for your help rudic. it works.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Scirpt to fetch the variable from sqlplus

Hi Gurus, I am stuck with the step where i need to fetch the location & sales from the below procedure by taking it from table field using the for loop. any idea how this can be done in unix. From one column both the location and sales are taken out. create or replace procedure newyork... (2 Replies)
Discussion started by: arun888
2 Replies

2. Shell Programming and Scripting

Csv format output file using scirpt

Hi All, I get the test result file daily after running the main test script. from the resultfile, need to fetch only server info and status and put them in tabular format in a file and as well in CSV format output file. I tried using awk command but am not able to put them in tabluar... (6 Replies)
Discussion started by: Optimus81
6 Replies

3. Shell Programming and Scripting

Help in writing the scirpt for grepping on patterns in the directories created previous day

Hi, I have a challenging requiremant to be done in one day:( there is directory in which a new directory gets created per hour i.e 24 directories per a day like below. Dec 01 00:04 2011.12.12-23 Dec 01 01:11 2011.12.10-07 Dec 01 01:11 2011.12.10-08 Dec 01 01:11 2011.12.10-09 Dec 01... (2 Replies)
Discussion started by: mahesh Madpathi
2 Replies

4. Shell Programming and Scripting

Error with using a shell command(looks more generic error)

Hi, This error is actually out of implementing the command posted here - https://www.unix.com/shell-programming-scripting/155589-remove-blank-lines-merge-lines-shell.html Here is the error i get - awk: Input line xxxxx cannot be longer than 3,000 bytes. The source line number is 1.... (1 Reply)
Discussion started by: dvah
1 Replies

5. Solaris

no shell error

i cannot login as a user then it shows a error. su: no shell i created a new user also. same error . cannot login user i changed shell also please help me urgently (6 Replies)
Discussion started by: sijocg
6 Replies

6. Shell Programming and Scripting

shell for..do done error

#!/bin/bash if ; then echo "error" fi release_name=$3 version=$4 cd $2; for line in `ls` do if ; then echo " do ..." $1 $2/$line $2/$line $release_name $version fi (3 Replies)
Discussion started by: crackthehit007
3 Replies

7. HP-UX

HPUX model scirpt issue

hi, I am using HPUX 11.31(Itanium) and HP 5100 printer. I am using PCL5.nloo model script present in HPUX by default to print to printer. I am finding issues in printing paper sizes and few other options. Description of the problem: When i give a print with option "half" and "Legal"... (4 Replies)
Discussion started by: meeraramanathan
4 Replies

8. UNIX for Dummies Questions & Answers

awk Shell Script error : "Syntax Error : `Split' unexpected

hi there i write one awk script file in shell programing the code is related to dd/mm/yy to month, day year format but i get an error please can anybody help me out in this problem ?????? i give my code here including error awk ` # date-month -- convert mm/dd/yy to month day,... (2 Replies)
Discussion started by: Herry
2 Replies

9. Shell Programming and Scripting

Using Public key in Shell scirpt

Hi, Can anyone help me out how to login on server using public key autorization.I want to use this on system. Thanks in advance. (1 Reply)
Discussion started by: LochanM
1 Replies

10. Shell Programming and Scripting

Perl scirpt for automatic file transfer

Hi , can any one help me with a perl script for automating file transfer ? At each step i need to check for the success or failure . I am trying to connect to the target system for a specified number of times.if it doesnt connect even after 3 retries then the script should exit. and i have to... (8 Replies)
Discussion started by: sveera
8 Replies
Login or Register to Ask a Question