LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-26-2016, 01:48 PM   #1
imaso001
LQ Newbie
 
Registered: Jul 2016
Posts: 6

Rep: Reputation: Disabled
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.
 
Old 07-26-2016, 02:01 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,026

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
what have you tried so far and where are you stuck ?

not sure what your question is ?
 
Old 07-26-2016, 02:26 PM   #3
imaso001
LQ Newbie
 
Registered: Jul 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-26-2016, 02:30 PM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,026

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
man mkdir
 
Old 07-26-2016, 02:35 PM   #5
imaso001
LQ Newbie
 
Registered: Jul 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-26-2016, 02:41 PM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,026

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
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
 
Old 07-26-2016, 02:51 PM   #7
imaso001
LQ Newbie
 
Registered: Jul 2016
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.
 
Old 07-26-2016, 10:47 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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.
 
Old 07-27-2016, 06:08 AM   #9
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,387

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
Thumbs up

Quote:
Originally Posted by grail View Post
The printing is a simple echo or printf command.
Lol, I applaud your patience
 
Old 07-27-2016, 06:33 AM   #10
wpeckham
Senior Member
 
Registered: Apr 2010
Location: USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, Vsido, tinycore, Q4OS
Posts: 1,648

Rep: Reputation: 568Reputation: 568Reputation: 568Reputation: 568Reputation: 568Reputation: 568
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Shell Scripting Part 3: Decision Control Structures in Shell Scripts LXer Syndicated Linux News 0 05-05-2015 11:01 PM
How do I make these shell scripts with ash as my shell? riahc3 Programming 13 08-15-2012 04:57 AM
best way to automate shell commands - shell script or python scripts or something els parangsaraf Linux - Newbie 11 08-08-2012 07:17 PM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 07:24 AM
need help on shell scripts!!! krcool32 Programming 5 04-28-2003 04:08 PM


All times are GMT -5. The time now is 10:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration