LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell scripts (https://www.linuxquestions.org/questions/linux-newbie-8/shell-scripts-4175585566/)

imaso001 07-26-2016 12:48 PM

Shell scripts
 
Hi, I am new to linuxquestions and have never dealt with shell scripts before. Please help

---------------------------
You’ll be working on this assignment in 3 stages.

2.1 Stage 1

1.Create a directory ~/UnixCourse/scriptAsst.


2.Within that directory, create a shell script, fileType.sh, 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 file 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.

For example:

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

Your script should not produce any other output when given a legal path to an existing file. (It may produce anything you like if given an incorrect path to a non-existent file.)

To see what kind of file you have, you can use the file command.

file filename

checks to see what kind of file is indicated by filename. It prints a description of its findings. For example, applying it to a Microsoft Word document yields:

file ~/UnixCourse/ftpAsst/macbeth.doc Composite Document File V2 Document, Little Endian, Os: Windows, Version 5.0, Code page: 1252, Author: Steven Zeil, Template: Normal.dot, Last Saved By: Steven Zeil, Revision Number: 1, Name of Creating Application: Microsoft Word 9.0, Total Editing Time: 11:00, Create Time/Date: Thu Oct 10 19:27:00 2002, Last Saved Time/Date: Thu Oct 10 19:38:00 2002, Number of Pages: 1, Number of Words: 77, Number of Characters: 440, Security: 0

If the output of the file command contains the phrase “ASCII text” and the phrase “CRLF”, then the file is Windows ASCII text.


3.When you believe that you have your script working, run
~cs252/bin/scriptAsst-crlf.pl


to check your work.


2.2 Stage 2

1.In that same directory, write a script checkFiles.sh that takes an arbitrary number of file paths from the command line and carries out the same analysis on each one, printing a line containing the file name, a colon, a blank, and then one of the two output strings from Stage 1, for each file in the command line.

For example,

./checkFiles.sh /usr/share/dict/words fileType.sh /home/cs252/Assignments/ftpAsst/d3.dat /usr/share/dict/words: Something else fileType.sh: Something else /home/cs252/Assignments/ftpAsst/d3.dat: Windows ASCII

When you believe that you have your script working, run
~cs252/bin/scriptAsst-crlf.pl


again to check your work.


2.3 Stage 3

1.In that same directory, write a script asciiFix.sh that takes an arbitrary number of file paths from the command line and carries out the same analysis on each one. If a file is not Windows ASCII, your script should do nothing to it. For each file that is Windows ASCII, your script should print the message

converting fileName

and should then convert the CR/LF line terminators in that file to Unix-style LF line terminators.

For example:

cp ~cs252/Assignments/ftpAsst/d3.dat wintest.txt ./asciiFix.sh /usr/share/dict/words wintest.txt fileType.sh
converting wintest.txt

and, after the script has finished, you should be able to determine that wintest.txt is now a Unix ASCII file.

schneidz 07-26-2016 01:01 PM

what have you tried so far and where are you stuck ?

not sure what your question is ?

imaso001 07-26-2016 01:26 PM

I began by creating a file in emacs, I am aware I have to use #!/bin/csh. I have not gotten any further. My question is how to create a shell script with the parameters listed. I am on stage 1.

--------

2.1 Stage 1

1.Create a directory ~/UnixCourse/scriptAsst.


2.Within that directory, create a shell script, fileType.sh, 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 file 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.

For example:

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

Your script should not produce any other output when given a legal path to an existing file. (It may produce anything you like if given an incorrect path to a non-existent file.)

To see what kind of file you have, you can use the file command.

file filename

checks to see what kind of file is indicated by filename. It prints a description of its findings. For example, applying it to a Microsoft Word document yields:

file ~/UnixCourse/ftpAsst/macbeth.doc Composite Document File V2 Document, Little Endian, Os: Windows, Version 5.0, Code page: 1252, Author: Steven Zeil, Template: Normal.dot, Last Saved By: Steven Zeil, Revision Number: 1, Name of Creating Application: Microsoft Word 9.0, Total Editing Time: 11:00, Create Time/Date: Thu Oct 10 19:27:00 2002, Last Saved Time/Date: Thu Oct 10 19:38:00 2002, Number of Pages: 1, Number of Words: 77, Number of Characters: 440, Security: 0

If the output of the file command contains the phrase “ASCII text” and the phrase “CRLF”, then the file is Windows ASCII text.

schneidz 07-26-2016 01:30 PM

man mkdir

imaso001 07-26-2016 01:35 PM

I know how to create a directory. How would I produce a script to examine that file and print a single line consisting of a phrase if the file is an ASCII text file with CR/LF line terminators or if the file is binary or ASCII with “Unix” LF line terminators.

schneidz 07-26-2016 01:41 PM

not sure if this would be cheating but my pc prints this out:
Code:

[schneidz@hyper win]$ file RHDSetup.log
RHDSetup.log: ASCII text, with CRLF line terminators


imaso001 07-26-2016 01:51 PM

More on the term of ...

The script should examine that file and print a single line consisting of the phrase

Windows ASCII - if the file 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.


I have created the file so far but how do I enable the script to print the above.

grail 07-26-2016 09:47 PM

The printing is a simple echo or printf command. The trick is to use some test to get the details you need and then use an 'if' conditional to print the necessary output.

schneidz has shown you a way to get the details needed.

keefaz 07-27-2016 05:08 AM

Quote:

Originally Posted by grail (Post 5581835)
The printing is a simple echo or printf command.

Lol, I applaud your patience

wpeckham 07-27-2016 05:33 AM

Emacs is a powerful editor and I consider it serious overkill for scripting, but it WILL serve the job.
A script is only a text file of shell commands, nothing more. There are features that make it more powerful than simply entering the commands at the shell prompt, but not a lot.

Were I you, I would read the section on parameters ($n where n is a number, etc.) as you will need to handle one or more such parameters in this script. I would then start with just a few lines using the commands (file obviously, then using something - possibly grep - to test the results) and work one step at a time towards having the result you need isolated.

Until you actually have a step that does not do what you want, or does something unexpected, there is no question here for us to answer. You have an assignment, and have posted it clearly, but no one here is ging to do it for you. You need to start: then either succeed or run into a question we can help you with.


All times are GMT -5. The time now is 08:25 PM.