I am a beginner for shell script, and I want to know how to check the content of file using if condition

I have a files content .txt and what I want is

if content of file have .txt; then
skip
else
run code
fi

Hello,

The first real question is what do you mean by content? Are you implying that it is a file containing only text or that it has the file extension .TXT?

And second is this real shell scripting in Linux, OSX and Unix, or Semi-scripting in Windows shell or VB?


To check whether the content of the file contains the string .txt:

file=content.txt
string=.txt
if grep -q "$string" "$file"
then
  : ## skip (i.e. do nothing)
else
  : do whatever
fi

To check whether a filename ends in .txt, use case:

file=whatever
case $file in
     *.txt) ;; # do nothing
     *) : do whatever ;;
esac

Hi,

I am beginner to shell scripts,and i want to know about checking the contents of shell scripts file which ends with the extension .ksh or .sh.

How we have to check the contents of those ready scripts used for execution purpose.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.