Shell script to read file and check file type


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to read file and check file type
# 1  
Old 06-20-2013
Shell script to read file and check file type

Hi,

I have a file with few values in it. I need script help to read file line by line and check:

1/if it's a file (with extension eg .java .css .jar etc )
or
2/if it's a file without extension and treat it as a directory and then check if the directory exists in working copy else create one

file example:
Code:
$cat test1
HelloWorldApplication/build.property
HelloWorldApplication/src/com/example/helloWorld/test.jar
test_merge.log
db2jcc.jar
log4j.jar
HelloWorldApplication/src/com
HelloWorldApplication/src/com/example
HelloWorldApplication/src/com/example/helloWorld
HelloWorldApplication/src/com/example/helloWorld/HelloWorld.java
HelloWorldApplication
.property


Last edited by iaav; 06-20-2013 at 02:47 PM..
# 2  
Old 06-20-2013
And what have you tried? Where are you stuck in your attempts at solving this problem?
# 3  
Old 06-20-2013
I tried doing 'while read" loop but bad errors and can't get it working.
# 4  
Old 06-20-2013
Quote:
Originally Posted by iaav
I tried doing 'while read" loop but bad errors and can't get it working.
Please elaborate. Post the script that you tried and failed. Post the errors that you got.
# 5  
Old 06-20-2013
Not close enough to post here. I just started with shell scripting

Thanks !!
# 6  
Old 06-20-2013
This is your file:
Quote:
[root@centostest2 20130620]# cat test1
HelloWorldApplication/build.property
HelloWorldApplication/src/com/example/helloWorld/test.jar
test_merge.log
db2jcc.jar
log4j.jar
HelloWorldApplication/src/com
HelloWorldApplication/src/com/example
HelloWorldApplication/src/com/example/helloWorld
HelloWorldApplication/src/com/example/helloWorld/HelloWorld.java
HelloWorldApplication
.property
/home/frank
/home/test1
and this is the script:
Code:
#!/bin/bash
# scriptname: check_01
# purpose: read the file "sample" line by line and check existence of files
# and directories

while read line
do
        if echo $line | grep -q '\.[a-zA-Z]\{1,\}$'
        then
                echo "$line is a file."
                if [ -e $line ]
                then
                        echo "$line exist."
                else
                        echo "$line doesn't exist."
                fi
        elif [ -e $line ]
        then
                echo "$line is a directory and it does exist."
        else
                echo "$line is a directory and it doesn't exist."
        fi
done < sample

cos I don't know what if the file or the directory doesn't exist so the script is not exactly what you need. However I think balajesuri is right and you should post your script as much as possible. I'm a newb too.
# 7  
Old 06-20-2013
Thanks Franksunn and balajesuri:

I worked on it.Got it to work but your script looks better:

Code:
while read line
do
case `basename "$line"` in
*.* )
      echo $line
      echo "$line is ext"
       ;;
* )
       echo $line
       [[ -d $line ]] || mkdir -p $line
       echo "$line is directory"
       ;;
esac
done <"test1"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell Script to Read the given file contents into a merged one file

Like to have shell script to Read the given file contents into a merged one file with header of path+file name followed by file contents into a single output file. While reading and merging the file contents into a single file, Like to keep the format of the source file. ... (4 Replies)
Discussion started by: Siva SQL
4 Replies

2. Shell Programming and Scripting

Check if a file type exists repeatedly

Hi, I have the below script that checks if the .tar file exists in my present directory. If so, extract it and then delete it except if the .tar filename is blent.tar #!/bin/bash while ; do echo "TAR BALL EXISTS" for f in *.tar; do echo "File -> $f" tar -xvf $f rm -ir $f... (7 Replies)
Discussion started by: mohtashims
7 Replies

3. Homework & Coursework Questions

Shell script to find file type

1. The problem statement, all variables and given/known data: Write a shell script that takes a single command line parameter, a file path (might be relative or absolute). The script should examine that file and print a single line consisting of the phrase: Windows ASCII if the files is an... (4 Replies)
Discussion started by: kwatt019
4 Replies

4. Shell Programming and Scripting

Shell script to read specified value from file and echo to the same location to other file.

Hello. I want to to backup some "default:" values from a file do some other job and after restore that "default:" values back. The problem is that the source and destination file has a lot of default: strings in it but with different values... So.. Here is an example: A part of my source... (6 Replies)
Discussion started by: ausdim
6 Replies

5. UNIX for Dummies Questions & Answers

Assistance with shell script to check file type and move to a folder.

Hi, Below is some code that I would like to implement however I am getting these errors: (what I am attempting to do is to check if a zip file has ascii files and if ascii and not binary then move the ascii files to a folder. some of the files are in xml format but are ascii and i will be moving... (0 Replies)
Discussion started by: bwcberb
0 Replies

6. Shell Programming and Scripting

Check file content type

I have a shell script that takes a file and uses "syncsort" to sort contents. I want to add a condition to check whether the input file is textual or binary format. If textual, the "syncsort" will be used to sort the files contents. Otherwise, the sorting process will be skipped. Note that the... (3 Replies)
Discussion started by: synthea
3 Replies

7. Shell Programming and Scripting

how to read dbf file in shell script and to convert dbf file usinf shell script

Hi all, I am new to shell scripting. I have dbf file and I need to convert it into csv file. OR, can i read the fields from a .dbf file and OR seprate the records in dbf file and put into .csv or txt. Actually in the .dbf files I am getting , the numbers of fields may vary in very record and... (6 Replies)
Discussion started by: gauara
6 Replies

8. Shell Programming and Scripting

Need shell script to read two file at same time and print out in single file

Need shell script to read two file at same time and print output in single file Example I have two files 1) file1.txt 2) file2.txt File1.txt contains Aaa Bbb Ccc Ddd Eee Fff File2.txt contains Zzz Yyy Xxx (10 Replies)
Discussion started by: sreedhargouda
10 Replies

9. Shell Programming and Scripting

how to check the file data type(ascii or binary)

hi i am receiving a file from one system , i have to verify the format of the file data i.e whether the data is in acii format or binary format, please help thanks in advance satya (1 Reply)
Discussion started by: Satyak
1 Replies

10. Shell Programming and Scripting

Have a shell script check for a file to exist before processing another file

I have a shell script that runs all the time looking for a certain type of file and then it processes the file through a series of other scripts. The script is watching a directory that has files uploaded to it via SFTP. It already checks the size of the file to make sure that it is not still... (3 Replies)
Discussion started by: heprox
3 Replies
Login or Register to Ask a Question