0

I am attempting to make a script that will check to see if there is any tyext within a file. I have developed the following script. I have made it check to see if there is exactly 2 arguments, see if the file exists, but I am having trouble checking the file for text within it. The code is as follows:

#!/bin/ksh
#check if number of arguments are 2

if [ $# -ne 2 ]; then
    echo "Does not equal two arguments"
    echo "Usage $0 inputfile outputfile"
    exit 1
fi

#check if input file exists

if [ ! -f $1 ]; then
    echo "$1 not found!"
    exit 1
fi

#Check if input file is null
#This next block of code is where the issue is
if [ grep -q $1 -eq 0 ]; then
    echo "$1 must have text within the file"
    exit 1
fi

Any help would be appreciated

2 Answers 2

1

test's "-s" option checks if the file is empty -- see manual. So your last chunk would become

#Check if input file is null
#This next block of code is where the issue is
if [ ! -s $1 ]; then
    echo "$1 must have text within the file"
    exit 1
fi
Sign up to request clarification or add additional context in comments.

Comments

0

Try using stat

stat -c %s filename

Comments

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.