LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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


Closed Thread
  Search this Thread
Old 10-16-2014, 04:52 PM   #1
LoganK
LQ Newbie
 
Registered: Oct 2014
Posts: 8

Rep: Reputation: Disabled
Sample question for school project


Write a script to be saved on our server as 'project1.sh' that does the following things:
Runs 1 step ever 2 seconds, displaying to the user what it is doing
Counts the number of files in the /tmp directory
Counts the number of files in the /tmp directory that end in '.log'
Counts the number of files that have a 1 character name
Saves all of the output to 'project1.data'
Displays the contents of 'project1.data' to the user when the script completes

Sample output from script being run:
[netravis@Main-server 2434]$ ./project1.sh
Counting the number of files in the /tmp directory
Counting the number of files in the /tmp directory that end in '.log'
Counting the number of files that only have a 1 character name
The results in order are:
20
1
9
[dwtraviss@Main-server 2434]$
 
Old 10-16-2014, 04:55 PM   #2
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,116

Rep: Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530
What is your question about this?
 
Old 10-16-2014, 04:58 PM   #3
LoganK
LQ Newbie
 
Registered: Oct 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
How write the script for this sample project. So far I have:

#!/bin/bash
echo "Counting the number of files in the /tmp directory"
ls /tmp | wc -l
echo "Counting the number of files in the /tmp directory that end in '.log'"
ls /tmp/*.log | wc -l
echo "Counting the number of files that only have a 1 character name"
 
Old 10-16-2014, 05:12 PM   #4
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,116

Rep: Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530Reputation: 1530
dupe.

http://www.linuxquestions.org/questi...9/#post5253834

Last edited by szboardstretcher; 10-16-2014 at 05:20 PM. Reason: removed answer
 
Old 10-16-2014, 05:15 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,051

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
dupe ?:
http://www.linuxquestions.org/questi...9/#post5253834
 
Old 10-16-2014, 05:19 PM   #6
LoganK
LQ Newbie
 
Registered: Oct 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thank you, but most of that was done by help from my teacher. Unfortunately he threw this at us without really explaining how to do it. I'm still learning basic commands. I'd like to be able to at least have this finished and then go back and take it apart piece by piece to understand it.
So yes, I'm at "counts of the number of files that have a 1 character name" then "saves all the output to project1.data" then "Displays the contents of 'project1.data' to the user when the script completes"

I also don't know how to complete the script...I know I use "fi" right? I'm just not sure where everything goes. I'm completely clueless to be quite honest.
It would be a huge help if you could help me complete the script and then I can study how everything works before the next class. Thank you!!!
 
Old 10-16-2014, 05:24 PM   #7
LoganK
LQ Newbie
 
Registered: Oct 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
My teacher told us that we don't need to use echo "Counting the number of files in the /tmp directory", echo "Counting the number of files in the /tmp directory that end in '.log'" , and echo "Counting the number of files that only have a 1 character name" but it's good to use for structure. If using that for the rest of the script, how would that look too?
 
Old 10-16-2014, 05:28 PM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
Quote:
Originally Posted by LoganK View Post
My teacher told us that we don't need to use echo "Counting the number of files in the /tmp directory", echo "Counting the number of files in the /tmp directory that end in '.log'" , and echo "Counting the number of files that only have a 1 character name" but it's good to use for structure. If using that for the rest of the script, how would that look too?
I don't understand the question.

If you want to print something, then type out what you want it to print and stick "echo" in front of it. Where is the confusion?
 
Old 10-16-2014, 05:31 PM   #9
LoganK
LQ Newbie
 
Registered: Oct 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Ah I see, so in completing the script for "Counts the number of files that have a 1 character name" "Saves all of the output to 'project1.data'" and "Displays the contents of 'project1.data' to the user when the script completes" I would just write echo before each of those with the command under each...correct?
 
Old 10-16-2014, 05:33 PM   #10
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,362

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
Quote:
Originally Posted by LoganK View Post
Thank you, but most of that was done by help from my teacher. Unfortunately he threw this at us without really explaining how to do it. I'm still learning basic commands. I'd like to be able to at least have this finished and then go back and take it apart piece by piece to understand it.
So yes, I'm at "counts of the number of files that have a 1 character name" then "saves all the output to project1.data" then "Displays the contents of 'project1.data' to the user when the script completes"

I also don't know how to complete the script...I know I use "fi" right? I'm just not sure where everything goes. I'm completely clueless to be quite honest.
It would be a huge help if you could help me complete the script and then I can study how everything works before the next class. Thank you!!!
Here are the basics:

"ls" is used to list directory contents. If you give it an argument, like "ls /tmp/", it will list the contents of "/tmp" instead of your current directory. You can also give it wildcards, like "ls /tmp/*.log" or "ls ???.dat" to only list the files/dirs that match that pattern.

"*" means "match zero or more characters"
"?" means "match exactly one character"

So "?.txt" would match "a.txt", "b.txt", and so on, but not "ab.txt".

"wc" is a command to count the number of words in the input. If you add the "-l" flag, then it counts the number of lines instead.

"cat" is a command to dump out the contents of a file to the terminal.

"echo" is a command to print to the terminal, so "echo 5" will print "5" to the terminal, while "echo hello there" will print "hello there" to the terminal.

"|" is used to redirect the output of one command to the input of another command.

">" is used to redirect the output of a command to a file. So if you run "ls > ls.out", instead of printing the directory listing to the terminal, it will dump the directory listing into the file "ls.out"




These six simple commands are all that are required to complete your assignment.




To take one of your current commands:
"ls /tmp/*.log | wc -l"
That means, list all files in /tmp that match the pattern "*.log". Then send the output to "wc -l", which will count the number of lines. Since ls prints one file per line, this will effectively count the number of files that matched your pattern.


"fi" is the end of an if statement. If you don't have any if statements in your script then you shouldn't have any "fi" either. If you want to end your script, just stop typing, you don't need any specific command or letter combination to end.


Furthermore - any command you can put in a script, you can also run on the terminal by itself. There's nothing special about it being in a script, a script is just a sequence of commands. So just start experimenting. Type "ls", see what the output is. Type "ls /tmp/" and see what the output is. Type "ls /tmp/ | wc -l" and see what the output is. Scripting is not a black box where you have to come up with the perfect commands with the perfect syntax the first time, with no practice, no experimentation, and hope you're right or it'll blow up the machine. Just start experimenting. See what commands do what, see how to use them, see how to combine them, play with it and see what happens. If you have questions, use "man", as in "man ls", "man echo", "man cat", "man wc", etc.

Last edited by suicidaleggroll; 10-16-2014 at 05:43 PM.
 
Old 10-16-2014, 09:01 PM   #11
LoganK
LQ Newbie
 
Registered: Oct 2014
Posts: 8

Original Poster
Rep: Reputation: Disabled
Lightbulb

Thank you so much. I'm definitely not trying to "dupe" anyone, and I want to learn the information, it's just a bit overwhelming. I'm going to try to understand what I have so far in order:

[B]"Runs 1 step ever 2 seconds, displaying to the user what it is doing" (Don't know how to do this yet) *please help*
echo (print) "Counting the number of files in the /tmp directory"
"Counts the number of files in the /tmp directory" - ls /tmp | wc -l - where ls is listing directory contents, /tmp is listing the contents of that directory, pipe "|" to redirect output of that command to the input of another command, wc is counting the number of words in the input, and -l to count the number of lines.
echo (print) "Counting the number of files in the /tmp directory that end in '.log'"
"Counting the number of files in the /tmp directory that end in '.log'" - ls /tmp/*.log | wc -l - where ls is listing directory contents, /tmp/ is listing the contents of that directory, * means to match zero or more characters, and .log is the directory in which I'm searching.
echo (print) "Counting the number of files that only have a 1 character name"
(Don't know how to do this yet) *please help* I'm going to guess that the command is - ls /tmp/?.log | wc -
Now I'm guessing again: echo "Saves all of the output to 'project1.data'"
(Don't know how to do this yet) *please help*
Now I'm guessing again: echo "Displays the contents of 'project1.data' to the user when the script completes"
(Don't know how to do this yet) *please help*



Once again the orginal series of questions for the script is:

Runs 1 step ever 2 seconds, displaying to the user what it is doing
Counts the number of files in the /tmp directory
Counts the number of files in the /tmp directory that end in '.log'
Counts the number of files that have a 1 character name
Saves all of the output to 'project1.data'
Displays the contents of 'project1.data' to the user when the script completes
Sample output from script being run:
[dwtraviss@Main-server 2434]$ ./project1.sh
Counting the number of files in the /tmp directory
Counting the number of files in the /tmp directory that end in '.log'
Counting the number of files that only have a 1 character name
The results in order are:
20
1
9
[dwtraviss@Main-server 2434]$
 
Old 10-17-2014, 06:22 AM   #12
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,260

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
This is a good cmd line guide http://rute.2038bug.com/index.html.gz

Definitely try to get one cmd/line working at a time; much easier to debug that way.
Also, use
Code:
set -xv
at the top of your script file; it'll show you exactly what's happening
 
Old 10-17-2014, 12:08 PM   #13
jeremy
root
 
Registered: Jun 2000
Distribution: Debian, Red Hat, Slackware, Fedora, Ubuntu
Posts: 12,231

Rep: Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate.

--jeremy
 
  


Closed Thread


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
School Project Please Help LoganK Linux - Newbie 7 10-17-2014 04:52 PM
LXer: Build a Sample Project with Eclipse Cell BE SDK LXer Syndicated Linux News 0 12-21-2006 01:33 AM
School Project Bovine64 Linux - Networking 2 04-17-2006 07:15 PM
I need a little help for a school project... Tsuroerusu General 25 12-12-2004 08:34 AM
My school project Growly Linux User Groups (LUG) 1 04-30-2004 05:30 PM


All times are GMT -5. The time now is 06:33 AM.

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