LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Sample question for school project (https://www.linuxquestions.org/questions/linux-newbie-8/sample-question-for-school-project-4175522356/)

LoganK 10-16-2014 03:52 PM

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]$

szboardstretcher 10-16-2014 03:55 PM

What is your question about this?

LoganK 10-16-2014 03:58 PM

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"

szboardstretcher 10-16-2014 04:12 PM

dupe.

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

schneidz 10-16-2014 04:15 PM

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

LoganK 10-16-2014 04:19 PM

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!!!

LoganK 10-16-2014 04:24 PM

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?

suicidaleggroll 10-16-2014 04:28 PM

Quote:

Originally Posted by LoganK (Post 5254813)
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?

LoganK 10-16-2014 04:31 PM

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?

suicidaleggroll 10-16-2014 04:33 PM

Quote:

Originally Posted by LoganK (Post 5254812)
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.

LoganK 10-16-2014 08:01 PM

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]$

chrism01 10-17-2014 05:22 AM

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

jeremy 10-17-2014 11:08 AM

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


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