LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Create a bash script that verifies an existing directory? (https://www.linuxquestions.org/questions/linux-newbie-8/create-a-bash-script-that-verifies-an-existing-directory-917895/)

AB1826 12-09-2011 08:28 AM

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!

TB0ne 12-09-2011 09:08 AM

Quote:

Originally Posted by AB1826 (Post 4545857)
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!

No, you need to do your own homework. We're not going to write your scripts for you, but we will be happy to HELP you...that means, YOU write the script, and if it doesn't work, or you're stuck...you post what YOU have written, and explain what's going wrong, and where you're stuck.

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/

AB1826 12-09-2011 09:18 AM

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

colucix 12-09-2011 09:45 AM

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.

colucix 12-09-2011 09:53 AM

Quote:

Originally Posted by AB1826 (Post 4545890)
What does he mean that i should run /usr as an argument

http://tldp.org/LDP/Bash-Beginners-G...#sect_03_02_05 where positional parameters == arguments.
Quote:

Originally Posted by AB1826 (Post 4545890)
and when i try to run it in the background with "&" it starts it in the background but the returns into the foreground....

Question: where do you expect the standard output of a command running in background is sent to? You sure it returns into foreground?

AB1826 12-10-2011 09:09 PM

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

colucix 12-11-2011 01:20 AM

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 &
[1] 25646
hello world
[1]+  Done                    echo hello world
$

You get three lines: the first one tells you the job is running in background as job 1 and the assigned process ID (PID) is 25646. The second line is the output of the command. The third line informs you that background job number 1 has finished.

AB1826 12-13-2011 02:54 AM

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!

catkin 12-13-2011 04:53 AM

What does echo /bin/n* do? Could it be extended to do what you want?

Hint: bash filename expansion, especially the Pattern matching section.

zQUEz 12-13-2011 07:39 AM

how about
Quote:

ls -a /bin/[a,b,n]* >~/commands_nb.lst

catkin 12-13-2011 08:42 AM

Quote:

Originally Posted by zQUEz (Post 4548720)
how about

The LQ Rules say "Do not expect LQ members to do your homework - you will learn much more by doing it yourself" and mostly we make the rule effective by not doing homework for people.

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
but the requirement was "a list of all filenames in the folder /bin that start with a n or b" so it would be more precisely solved by
Code:

cd /bin
echo [abn]* >~/commands_nb.lst

or, less minimally and less accurately if there are files with unusual characters in their names by:
Code:

cd /bin
ls -1 [abn]* >~/commands_nb.lst

BTW it's better to put code in CODE tags that QUOTE tags so they are shown in fixed pitch font and appear when somebody quotes the post when replying.

AB1826 12-13-2011 09:49 AM

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.