Create a bash script that verifies an existing directory?
Hi! I have an assignment that i have some trouble with.
Here is the assignment: "Create a bash script called script1 in the home directory of user ide. This script should verify that the first command line argument is a name of an existing directory. If it is not, the script should exit with an error message. Otherwise, it should start the command “tar –czf /tmp/bak.tgz X”, where X is that directory" Can someone write the script and if possible explain it? Thanks! |
Quote:
If you really can't do your own homework, then tell your teacher that you need help. Try Google for scripting tutorials/examples, like this one: http://tldp.org/LDP/abs/html/ |
No problem
Yeah, i have actually managed to do the script!, but now im stuck at the additional assignment.
Here is the script: #!/bin/bash DIRECTORY=/usr if [ -d $DIRECTORY ]; then sudo tar -czf /tmp/bak.tgz $DIRECTORY else echo "Lukas did it!" exit fi Now the next assignment says, : . Run the script script1 as a background task, giving “/usr” as an argument. After the script is done, make sure it worked! What does he mean that i should run /usr as an argument, and when i try to run it in the background with "&" it starts it in the background but the returns into the forground.... |
Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.
|
Quote:
Quote:
|
Okay, so i can clear up some things, i did not load up any homework here.
I have a practical exam next week. We have got à bunch of example questions from last year and we are stuck st question 7 and 15. Here is the link for our course web page : www.hh.se/do2003 . If you look at his latent update, you will se that there are example questions for the practical. I appriciate all the help and the answers but i do not want you to think that you are doing some homework for me! I am just trying to understand how it is done. P.S If you were my teacher, you would recognize your own questions and just confirm what i wrote in this post. Now, can you please help me?!?!?! |
Ok. You've made your point! Let's focus on the questions, now. The link I posted above should have clarified things about positional parameters, a.k.a. arguments. Looking at your course material you may also follow the links Related Material -> Bash Tutorial -> 4. Passing arguments to the bash script. What are still your doubts about this?
Regarding the background processes, the answer is in my question above. I repeat for the sake of clarity: where do you expect the standard output of a command running in background is sent to? You sure it returns into foreground? Please look at this: Code:
$ echo hello world & |
Thanks!
Actually, your link and your description explained alot! Awesome!
What i need help with is the last assignment, because i understand the rest now! The last assignment says: Create a file (in your home directory) that contains a list of all filenames in the folder /bin that start with a n or b. The file you create should be named commands_nb.lst So, i cannot make it work totally as i would like to. Here is how i have done it so far: ls -a /bin | grep ^a > commands_nb.lst It only puts in one file name in the list, but i would like it to put in all filenames that start with a. And i would also like to run the command for a, n and b only one time. So i dont have to run the command for each letter.. I have a total blackout for the command.... Thanks alot for the help! Amazing! |
What does echo /bin/n* do? Could it be extended to do what you want?
Hint: bash filename expansion, especially the Pattern matching section. |
how about
Quote:
|
Quote:
The command you suggested will work but neither the -a nor the commas are necessary. This is a cleaner solution: Code:
ls /bin/[abn]* >~/commands_nb.lst Code:
cd /bin Code:
cd /bin |
I solved thanks to you guys!
I did: ls -a /bin | grep ^[a,n,b] > Filename Awesome! Thanks alot for the help! |
All times are GMT -5. The time now is 08:36 PM. |