Sponsored Content
Homework and Emergencies Homework & Coursework Questions Shell script to find file type Post 302925366 by kwatt019 on Sunday 16th of November 2014 09:18:30 PM
Old 11-16-2014
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 ASCII text file with CR/LF line terminators, or
Something else
if the file is binary or ASCII with “Unix” LF line terminators.


2. Relevant commands, code, scripts, algorithms:

Output should look like this:
./fileType.sh ~cs252/Assignments/ftpAsst/d3.dat
Windows ASCII
./fileType.sh /bin/cat
Something else
./fileType.sh fileType.sh
Something else
./fileType.sh /usr/share/dict/words
Something else

3. The attempts at a solution (include all code and scripts):
The first attempt I tried worked on files, but when I ran it on folders it outputted Windows ASCII. The second attempt I tried works, but only outputs Windows ASCII.
Code:
#!/bin/sh 
file=$1 
if grep -q "\r\n" $file;then 
echo Windows ASCII 
else 
echo Something else 
fi

and I have tried this:
Code:
#!/bin/sh
if test -f $file;
then
echo "Windows ASCII"
else
echo "Something else"
fi

Third attempt.
Code:
#!/bin/sh
file=$1
case $(file $file) in
*"ASCII test, with CRLF lin terminators")
echo "Windows ASCII"
;;
*)
echo "Something else"
;;
esac



4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Old Dominion University, Norfolk VA USA, Professor Steven Zeil, CS 252

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Last edited by kwatt019; 11-17-2014 at 12:27 AM.. Reason: added third attempt.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help on Simple shell that look for a file type

Hello Everyone, I got a simple question. I'm trying to write a shell that looks for a file that arived ftp area and keeps looking until it find it. It should look for the file that begins with test and then when it finds it. It starts the wmdaten.sh shell. All i got is that it runs... (5 Replies)
Discussion started by: Peterh
5 Replies

2. Shell Programming and Scripting

Specifying font type and color in a shell script

Hi, I am new to shell script. Can you please tell me, whether can we specify font type and color in a shell script? If so, how to do this? Thanks in advance. (4 Replies)
Discussion started by: Vani_Govind
4 Replies

3. Shell Programming and Scripting

converting the data type in unix shell script

I am writing a unix shell script that will extract records from table and write into a file. ====================================== #! /bin/ksh ############################ # AFI Monitor Script ############################ . /db2/uszlad48/sqllib/db2profile export... (5 Replies)
Discussion started by: kmanivan82
5 Replies

4. Shell Programming and Scripting

How to type ISO_8859_1 characters in shell script?

Hello All, I want to type some special characters to check in my shell script as string. Could you please help me how do I type in my shell script. For example "LA CORUĆA". Here the last but 1 character is related to ISO_8859_1 related. But don't know how do I type in shell script programming.... (3 Replies)
Discussion started by: nvkuriseti
3 Replies

5. Shell Programming and Scripting

How to find yesterdays file - shell script

Hey guys - i have a script (below) that searches for current files in a particular directory. However i was wondering how to make it search for "yesterdays" file. For instance it looks for a file from yesterday and no older than that. I used stat command to check for file information: ... (6 Replies)
Discussion started by: DallasT
6 Replies

6. 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

7. Shell Programming and Scripting

Shell Script Find in File

Right, noob to shell scripting, playing a round for practice, wrote the following but it doesn't seem to work as expected, how could I fix/improve this script? #!/bin/bash #set -v #set -x case $# in 1) echo Searching for $1 in '*'; find . -iname '*' 2>/dev/null | xargs grep "$1" -sl... (3 Replies)
Discussion started by: Pezmc
3 Replies

8. UNIX for Dummies Questions & Answers

Shell script find word from one file and insert in another file

Hi, I am new to shell scripting. I need a bash shell scripts which search and grep a parameter value from input.txt file and insert it in between two semicolon of second line of output.txt file. For example The shell script search an IP address as parameter value from input.txt ... (2 Replies)
Discussion started by: sunilkumarsinha
2 Replies

9. Shell Programming and Scripting

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... (6 Replies)
Discussion started by: iaav
6 Replies

10. Shell Programming and Scripting

Find file type recursively and move

Hello, I supposed that it was working fine but now I see that it's not working as expected. I am running under ubuntu14.04, trusty. My plan was to search folderA and all subdirectories and move any txt file to destination folder, folderB : find /home/user/folderA/ -type f -iname "*.txt"... (0 Replies)
Discussion started by: baris35
0 Replies
IMAP_MIME_HEADER_DECODE(3)						 1						IMAP_MIME_HEADER_DECODE(3)

imap_mime_header_decode - Decode MIME header elements

SYNOPSIS
array imap_mime_header_decode (string $text) DESCRIPTION
Decodes MIME message header extensions that are non ASCII text (see RFC2047). PARAMETERS
o $text - The MIME text RETURN VALUES
The decoded elements are returned in an array of objects, where each object has two properties, charset and text. If the element hasn't been encoded, and in other words is in plain US-ASCII, the charset property of that element is set to default. EXAMPLES
Example #1 imap_mime_header_decode(3) example <?php $text = "=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@example.com>"; $elements = imap_mime_header_decode($text); for ($i=0; $i<count($elements); $i++) { echo "Charset: {$elements[$i]->charset} "; echo "Text: {$elements[$i]->text} "; } ?> The above example will output: Charset: ISO-8859-1 Text: Keld Jorn Simonsen Charset: default Text: <keld@example.com> In the above example we would have two elements, whereas the first element had previously been encoded with ISO-8859-1, and the second element would be plain US-ASCII. SEE ALSO
imap_utf8(3). PHP Documentation Group IMAP_MIME_HEADER_DECODE(3)
All times are GMT -4. The time now is 02:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy