LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Identifying files in bash script (http://www.linuxquestions.org/questions/programming-9/identifying-files-in-bash-script-157901/)

Cruger 03-15-2004 08:18 AM

Identifying files in bash script
 
Hi...
I'm writing a bash script, but I've run into some trouble..

I need the script to identify what type of file it recieves... for example if the file in question is Filename.txt, then I need the script to identify that it is a .txt-file... the same goes for any type (.zip, .tar....)

Does anyone understand what I'm asking:confused: I have trouble continuing the script whitout this...

See ya on the flipside!!

Bebo 03-15-2004 08:42 AM

You can check the command file, but from your post it seems that you're not after that kind of info. So, if you're just after the extension of the file, maybe something like this is ok:
Code:

#!/bin/sh

ls | grep '\.' | while read FILE ; do
  EXTENSION=`echo $FILE | rev | tr '\.' '\ ' | cut -f1 -d' ' | rev`
  echo "$FILE has extension $EXTENSION"
done

A bit cumbersome perhaps, but it shows you the idea.

HTH

Cruger 03-15-2004 08:54 AM

:study:

Najjs Bebo... now I have something to work with :D

That one tells me what all my files have for extention, now all I have to do is rewrite it so it only looks at the file in question (for example $1), thanks for the tip !!

Bebo 03-15-2004 09:03 AM

Grrreat, glad I could help ya :)

Cruger 03-15-2004 10:34 AM

AAAhhhrrgggggg....

This is hopeless !!! I've tried to put the ls | grep '\.' command in a for and an if-loop to try and identify ONLY the file I'm sending to the script... but I keep messing it up.. it lists all files of that kind.. but I need it to only compare it... in other words to see if it "fits" the file-type I'm looking to compare it to...

If I send in a file: Filename.txt and my script looks at it, it should only be answering true or false... what am I doing wrong??? "Sighhhhh" :cry:

hiteshmaisheri 03-15-2004 11:16 AM

can you just post your script here... if you can please do that...

Cruger 03-15-2004 11:21 AM

Here comes part of the script:

..............................................................................
ls | grep '\.'
if [ $1 = "\.txt" ]
then
echo " This is a .txt file"
else
echo "This is not a .txt file"
fi
...............................................................................

bigearsbilly 03-15-2004 11:49 AM

Code:


for file in * ;do

    case ${file##*.} in

    txt) echo "$file is a TXT file"
          ;;

    c) echo "$file is a C file"
          ;;

    *)
        echo "$file is UNKNOWN"
        ;;

    esac
done

yields:

billym.primadtpuat> tt
test.c is a C file
wozzle is UNKNOWN
wozzle.c is a C file
wozzle.txt is a TXT file
billym.primadtpuat>


All times are GMT -5. The time now is 08:18 AM.