0

I am trying to write a file that mimics the cat -n bash command. It is supposed to accept a filename as input and if no input is given print a usage statement.

This is what I have so far but I am not sure what I am doing wrong:

#!/bin/bash
echo "OK"
read filename
if [ $filename -eq 0 ]
then 
    echo "Usage: cat-n.sh file"
else
    cat -n $filename
fi

1 Answer 1

2

I suggest to use -z to check for empty variable $filename:

if [ -z $filename ]

See: help test

Sign up to request clarification or add additional context in comments.

1 Comment

Or -f to test for the existence of a regular file or -r to verify that it is readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.