Justin Reid
Justin Reid

Reputation: 119

Checking input with an if statement (bash)

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

Upvotes: 0

Views: 105

Answers (1)

Cyrus
Cyrus

Reputation: 88626

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

if [ -z $filename ]

See: help test

Upvotes: 2

Related Questions