Testing for an empty UNIX file using KSH on HP-UX

Have the following code. The “$billing-file” is /work1/brs/billing_bal/filename.txt. The problem is that the script aborts when attempting to execute that test command just as if an exit command was executed. I have an identical test command but with the -r option for the same file, and it does not terminate the script. So why would the -r option work, but the -s option causes the script to abort?

<if test -s “$billing-file”>
then
        some code
else
        more code
fi

Hello,

It would be helpful to know a bit more about your operating environment here to be able to give a conclusive answer. At a minimum, your operating system and shell would be what we'd really want to know here to have a better chance of being able to help you.

However, broadly speaking, I cannot see why the test command should cause a script to exit directly. The -s flag of test checks that a file exists and has a size greater than zero bytes; and the -r flag checks that a file exists and and is readable by the user running the test command. Neither of these checks on their own should cause a script to exit, though they could certainly cause your script to branch to a point where exiting would be the next logical step (either because there is explicitly an exit statement somewhere, or because the script leaves the if statement and then exits because there are no more commands to execute).

Assuming this is a Bash script, try running bash -x <path to your script> and see what you see happening step-by-step. That may be helpful in understanding what is going on here, and why. But failing that, if you can come back to us with information on exactly what your OS and shell are, and also provide the full output of a sample of you running the script (as well as the output of an ls -l on any files you are testing with the test command), and then we can take things from there.

Hope this helps !

any reason why there're < and > leading/trailing the if expression?
Copy/paste error or you actually have these in the if statement? They shouldn't be there...

Also should be " - double quotes....

2 Likes

Thank for responding. Just ignore the <>. They are not part of the actual code. Not sure why I put them in. Anyway, don't understand what you meant by using double quotes. Can you clarify that for me with an example?

Thanks for responding. It’s a HP UNIX environment. Also, it’s not a BASH script. I think it is just an ordinary shell script, probably ksh. I’ll get the other info you requested tomorrow morning, which I believe you are asking for the complete source code. By the way, I have been admonished a couple of times to enclose each statement with something that makes it easier for you to copy and paste. Is that correct?

Hello,

Thank you. The shell in use on HP-UX is likely to be either the default Bourne shell or (as you say) the Korn shell. Either way, the syntax for an if statement is broadly speaking the same, so it should not matter too much in this case. But if you could confirm when you get the chance, that would be great.

As for how to enclose code and script output - the easiest way is to bound the entire section in either [CODE] or ``` tags. So for example, your post here was edited so that the section quoting your script code was written as:

[CODE]
<if test -s “$billing-file”>
then
        some code
else
        more code
fi
[/CODE]

As per @vgersh99's reply to you, if we are talking about a regular shell script, then the angle brackets on the if line should not be there, and if they are genuinely in your script, could well be causing you problems of various kinds.

Hope this helps !

Hi @rtb

This is a technical forum. Your script is not ksh as ksh does not use angle brackets for if statements to my knowledge. Please provide a valid reference to verify why you"think" this is ksh; because I think not.

Please tell us exactly what scripting language this code is written in.

I don't think this is a valid assumption. Bash syntax for if statements use square brackets, not angle brackets.

The first step is to slow down, take a step back, and identify what program executes the following code:

<if test -s “$billing-file”>
then
        some code
else
        more code
fi

What is this script? Who wrote it? Where did it come from?

I have never seen any unix-like script which uses angle brackets for if statements.

Instead of guessing, let's just get the facts, please.

Thanks.

Note: I searched the net and could not find a single programming language which uses angle brackets for if statements in the manner offered in this original post. Please correct me if there is one. Thanks again.

Hey @rtb

What are you doing?

You are posting code asking for help, and they you say "ignore the code" and "you don't know why you posted it".

Why then, did you post it?

What are you doing? Why are you posting code which does not work ? For what reason? Did you just "make it up"? Do you ever look at the syntax of a language when coding?

Coding is simple. There is no need to guess.

If you want to program an if statement in bash, and you have no idea how to do it, first you need to google, for example:

if statement in bash

I just did it and there was nearly 41 million hits. Surely, you can find how write a simple if statement, right?

My guess is that @rtb wants to write a simple bash script but does not want to google to find out how to write his first if statement.

@rtb , as you know, I write a lot of code. When we write code, if we want some statement and we are not sure how to write it, first we go to a reference (on the net) which explains how the statement works and the syntax for the language we want.

What language do you want to write your script in?

I assume bash is that right?

OK, now we know after a lot of guessing and wasted brain power, @rtb has kindly revealed he is using KSH on HP-UX ! Wow :). We finally got the basic info we need, so I changed the topic and moved the topic to HP-UX :slight_smile:

We can all stop guessing, yea!

So, you cannot use angle brackets in this manner in KSH

<if test -s “$billing-file”>

In KSH, you need this type of syntax (from a google search to be sure), notice the square brackets.

 if [[ condition ]]; then
         // Condition satisfied and run commands between if..fi
 fi

Kindly find a good KSH reference and use that as a guide, @rtb and post back when you have actually written some of your own KSH code to address your task. The reference below provides nearly everything you need to write KSH code to test for an empty string.

Thanks!

Reference:

https://www.cyberciti.biz/faq/ksh-if-command-examples/

Note: Members (users asking questions) MUST specify your shell and OS when posting questions in a technical forum, otherwise you are wasting everyones time and energy. Time is precious. Team members should not guess or assume. It's better to know these things. If people do not post this basic information, ask for it "up front" and wait for the member to reply with this basic information, please. THANKS

The general form is
if list
then list
fi
Where list can be one a more statements.
And a statement can be a command like test or a command-like compound like [[ ]].
So it is correct to have

if test -s "$billing_file"
then
  echo "not empty"
fi

Notes:
A variable name must not contain a dash character, $billing-file is wrong. An underbar is correct: $billing_file.
The quotes must be ASCII quotes, not some multibyte-pretty-quotes that some modern editors support. Use a plain ASCII editor like vi or nano!
cat your script and use copy/paste to show it here. And paste it between triple-backticks markups (as provided by the little terminal symbol at the top of the Wiki editor).

2 Likes

Just one note: The default shell on HP-UX is /bin/sh which does not point to a Bourne Shell, but to the Posix Shell (see man sh-posix). Unless ksh is specifically called, this is most likely the shell that is being used.

( although in practice on HP-UX there are only a few difference with ksh 88, since the Posix Shell is derived from it and not all ksh functionality has been disabled)

2 Likes

Just an FYI here. The problem with the test command using the -s option to see if the file had data in it, was resolved by creating a function with that block of code in it. The execution of that function did not abort the script. Instead, it provided the correct results.

@rtb,
hhhhm... Would you mind sharing the solution?
I (for one) am having hard time to understand how the function with the same code could "fix" your issue.......
Would be curious to see the solution.......

2 Likes

@rtb

  • -r Return true if file is readable by the user

  • -s Return true if file exists, and is not empty

Not sure if I have used Markdown correctly as have never had to use it before, but here is the code bracketed with ``` at the start and at the end. If I need to do something different, please show me an example.

Anyway, the first group of code starting with "if test -r "$billing_file"" works just fine. The second group of code starting with "if test -s "$billing_file"" when executed, causes the script to abort. So the 3rd grouping of code that starts with "test_billing_file()" creates the function test_billing_file containing the same code in the 2nd grouping of code. At the end of the 3rd group, the function test_billing_file executes as I expected it to, without aborting the script.

################################################################
#Test to see if the billing system file is present and is
#readable using the -r option.
################################################################

if test -r "$billing_file"
then echo " " >> $logfile
     echo "The billing file - $billing_file - does exist"     >> $logfile
     echo " " >> $rpt_name
     echo "The billing file - $billing_file - does exist"     >> $rpt_name
else banner error
     echo " "                                                 >> $rpt_name
     echo "The billing file - $billing_file - does not exist" >> $rpt_name
     echo " " >> $logfile
     echo "The billing file - $billing_file - does not exist" >> $logfile
     echo "\nPress Enter to terminate...\c"
     read ans
     exit 1
fi

### This section of code is commented out as it is causing a
### premature exit of this script, before the actual report can be
### created.

#if test -s "$billing_file"
#then echo " "								>> $logfile
#    echo "The $billing_file exists and has size greater than zero" 	>> $logfile
#    echo " "								>> $rpt_name
#    echo "The $billing_file exists and has size greater than zero" 	>> $rpt_name
#else banner error
#     echo " "						 >> $logfile
#     echo "The billing file - $billing_file - is empty  >> $logfile
#     echo " "                                           >> $rpt_name
#     echo "The billing file - $billing_file - is empty" >> $rpt_name
#     echo "\nPress Enter to terminate...\c"
#     read ans
#     exit 1
#fi

################################################################
#
#Since the code above this was commented out, another method
#is being tried.
#
#A function called 'test_billing_file' is created with the
#code between the { and the }. This function will be
#executed right after the function is created.
#
#This will determine if the billing file that exists has
#data in it.
################################################################

test_billing_file()
{
if test -s "$billing_file"
then echo " "								>> $logfile
     echo "The $billing_file exists and has size greater than zero" 	>> $logfile
     echo " "								>> $rpt_name
     echo "The $billing_file exists and has size greater than zero" 	>> $rpt_name
else banner error
     echo " "						 >> $logfile
     echo "The billing file - $billing_file - is empty"  >> $logfile
     echo " "						 >> $rpt_name
     echo "The billing file - $billing_file - is empty"  >> $rpt_name
     echo "\nPress Enter to terminate...\c"
     read ans
     exit 1
fi
}

test_billing_file
1 Like

do you notice anything different in the structure of this line to the others ....
there's a missing closing double quote after the 'is empty' , i suggest you sort that then try running the script again - commenting out the call to the test_billing_function (which doesn't have missing quotes)

also, the following line doesn't really work - it doesn't fail to print but the \n and \c (what is that supposed to mean) are displayed

echo "\nPress Enter to terminate...\c"
2 Likes

@rtb,
well let's see.

here's your first "group" of code as a separate script - rtb1.sh:

#!/bin/bash

billing_file='billing_file'
rpt_name='rpt_name'
logfile='logfile'

################################################################
#Test to see if the billing system file is present and is
#readable using the -r option.
################################################################

if test -r "$billing_file"
then echo " " >> $logfile
     echo "The billing file - $billing_file - does exist"     >> $logfile
     echo " " >> $rpt_name
     echo "The billing file - $billing_file - does exist"     >> $rpt_name
else banner error
     echo " "                                                 >> $rpt_name
     echo "The billing file - $billing_file - does not exist" >> $rpt_name
     echo " " >> $logfile
     echo "The billing file - $billing_file - does not exist" >> $logfile
     echo "\nPress Enter to terminate...\c"
     read ans
     exit 1
fi

here's your second "group" of code as a separate script - rtb2.sh:

#!/bin/bash

billing_file='billing_file'
rpt_name='rpt_name'
logfile='logfile'

### This section of code is commented out as it is causing a
### premature exit of this script, before the actual report can be
### created.

if test -s "$billing_file"
then echo " "                               >> $logfile
    echo "The $billing_file exists and has size greater than zero"  >> $logfile
    echo " "                                >> $rpt_name
    echo "The $billing_file exists and has size greater than zero"  >> $rpt_name
else banner error
     echo " "                        >> $logfile
     echo "The billing file - $billing_file - is empty  >> $logfile
     echo " "                                           >> $rpt_name
     echo "The billing file - $billing_file - is empty" >> $rpt_name
     echo "\nPress Enter to terminate...\c"
     read ans
     exit 1
fi

running with non-existent bulling_file.

$  ./rtb1.sh
./rtb1.sh: line 17: banner: command not found
\nPress Enter to terminate...\c

./rtb2.sh
./rtb2.sh: line 21: unexpected EOF while looking for matching `"'
./rtb2.sh: line 26: syntax error: unexpected end of file

Looks like ./rtb1.sh is as expected.
Looks like ./rtb2.sh has some "syntax" errors and it aborts.

Looking at ./rtb2.sh, it looks like you're missing closing double quotes in the else block of your if :

echo "The billing file - $billing_file - is empty  >> $logfile

these double quotes are present in your your other scripts/groups...

Fix those and you'll get the same behavior in rtb2.sh as you did in rtb1.sh - no need for a function....

2 Likes

Closed for users (regular)

Staff can continue to discuss and comment.