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

**URL:** <https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104>\
**Category:** HP-UX\
**Created:** [April 5, 2022, 9:22pm UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104 "2022-04-05T21:22:38Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![rtb](https://community.unix.com/letter_avatar/rtb/32/6_22734026db12803ebb3e059622ac3534.png) [@rtb](https://community.unix.com/u/rtb)\
**Post date:** [April 5, 2022, 9:22pm UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104/1 "2022-04-05T21:22:38Z")

</div>

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?

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

---

<div class="post-metadata">

**Author:** ![drysdalk](https://community.unix.com/letter_avatar/drysdalk/32/6_22734026db12803ebb3e059622ac3534.png) [@drysdalk](https://community.unix.com/u/drysdalk)\
**Post date:** [April 5, 2022, 10:18pm UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104/2 "2022-04-05T22:18:17Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [April 5, 2022, 10:22pm UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104/3 "2022-04-05T22:22:45Z")

</div>

> [@rtb](#):
>
> ```plaintext
> <if test -s “$billing-file”>
> then
> some code
> else
> more code
> fi
> 
> ```

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....

---

<div class="post-metadata">

**Author:** ![rtb](https://community.unix.com/letter_avatar/rtb/32/6_22734026db12803ebb3e059622ac3534.png) [@rtb](https://community.unix.com/u/rtb)\
**Post date:** [April 6, 2022, 12:47am UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104/4 "2022-04-06T00:47:49Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rtb](https://community.unix.com/letter_avatar/rtb/32/6_22734026db12803ebb3e059622ac3534.png) [@rtb](https://community.unix.com/u/rtb)\
**Post date:** [April 6, 2022, 12:57am UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104/5 "2022-04-06T00:57:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![drysdalk](https://community.unix.com/letter_avatar/drysdalk/32/6_22734026db12803ebb3e059622ac3534.png) [@drysdalk](https://community.unix.com/u/drysdalk)\
**Post date:** [April 6, 2022, 1:59am UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104/6 "2022-04-06T01:59:46Z")

</div>

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:

```nohighlight
[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 !

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [April 6, 2022, 3:19am UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104/7 "2022-04-06T03:19:35Z")

</div>

> [@rtb](#):
>
> I think it is just an ordinary shell script, probably ksh.

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.

> [@drysdalk](#):
>
> Assuming this is a Bash script, try running

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:

```plaintext
<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.

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [April 6, 2022, 3:28am UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104/8 "2022-04-06T03:28:48Z")

</div>

> [@rtb](#):
>
> Thank for responding. Just ignore the \<\>. They are not part of the actual code. Not sure why I put them in

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:

```plaintext
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?

 ![Screen Shot 2022-04-06 at 10.09.06 AM](https://community.unix.com/uploads/default/original/3X/7/8/78c5a1c2228b788fc36fb3fab85a7cf23d082a17.png)

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?

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [April 6, 2022, 4:29am UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104/9 "2022-04-06T04:29:15Z")

</div>

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 🙂

We can all stop guessing, yea!

> [@Concatenate two values using KSH on HP-UX](https://community.unix.com/t/concatenate-two-values-using-ksh-on-hp-ux/386071/7):
>
> The operating system is HP-UNIX. The scripting is not BASH, but KSH. I’ll try to be more precise when looking for something on the internet.

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

```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.

```ksh
 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/](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_

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_22734026db12803ebb3e059622ac3534.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [April 6, 2022, 8:45am UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104/10 "2022-04-06T08:45:32Z")

</div>

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

```nohighlight
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).

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [April 6, 2022, 8:02pm UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104/11 "2022-04-06T20:02:23Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![rtb](https://community.unix.com/letter_avatar/rtb/32/6_22734026db12803ebb3e059622ac3534.png) [@rtb](https://community.unix.com/u/rtb)\
**Post date:** [April 6, 2022, 8:23pm UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104/12 "2022-04-06T20:23:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [April 6, 2022, 8:26pm UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104/13 "2022-04-06T20:26:22Z")

</div>

@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.......

---

<div class="post-metadata">

**Author:** ![munkeHoller](https://community.unix.com/user_avatar/community.unix.com/munkeholler/32/7431_2.png) [@munkeHoller](https://community.unix.com/u/munkeHoller)\
**Post date:** [April 6, 2022, 9:58pm UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104/14 "2022-04-06T21:58:39Z")

</div>

@rtb

- -r Return true if _file_ is readable by the user

- -s Return true if _file_ exists, and is not empty

---

<div class="post-metadata">

**Author:** ![rtb](https://community.unix.com/letter_avatar/rtb/32/6_22734026db12803ebb3e059622ac3534.png) [@rtb](https://community.unix.com/u/rtb)\
**Post date:** [April 6, 2022, 11:02pm UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104/15 "2022-04-06T23:02:36Z")

</div>

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.

```nohighlight
################################################################
#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

```

---

<div class="post-metadata">

**Author:** ![munkeHoller](https://community.unix.com/user_avatar/community.unix.com/munkeholler/32/7431_2.png) [@munkeHoller](https://community.unix.com/u/munkeHoller)\
**Post date:** [April 6, 2022, 11:37pm UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104/16 "2022-04-06T23:37:15Z")

</div>

> [@rtb](#):
>
> `# echo "The billing file - $billing_file - is empty >> $logfile`

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

```plaintext
echo "\nPress Enter to terminate...\c"

```

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [April 6, 2022, 11:42pm UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104/17 "2022-04-06T23:42:56Z")

</div>

@rtb,  
well let's see.

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

```bash
#!/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`:

```bash
#!/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`.

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

```

```bash
./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` :

```bash
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....

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [April 7, 2022, 1:14pm UTC](https://community.unix.com/t/testing-for-an-empty-unix-file-using-ksh-on-hp-ux/386104/24 "2022-04-07T13:14:49Z")

</div>

Closed on April 7, 2022, 1:14pm UTC

Closed for users (regular)

Staff can continue to discuss and comment.
