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 12-09-2011, 09:28 AM   #1
AB1826
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Rep: Reputation: Disabled
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!
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 12-09-2011, 10:08 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,942

Rep: Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693Reputation: 3693
Quote:
Originally Posted by AB1826 View Post
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/
 
2 members found this post helpful.
Old 12-09-2011, 10:18 AM   #3
AB1826
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
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....
 
Old 12-09-2011, 10:45 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
Old 12-09-2011, 10:53 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by AB1826 View Post
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 View Post
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?
 
1 members found this post helpful.
Old 12-10-2011, 10:09 PM   #6
AB1826
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
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?!?!?!
 
Old 12-11-2011, 02:20 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
1 members found this post helpful.
Old 12-13-2011, 03:54 AM   #8
AB1826
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
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!
 
Old 12-13-2011, 05:53 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

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

Hint: bash filename expansion, especially the Pattern matching section.
 
Old 12-13-2011, 08:39 AM   #10
zQUEz
Member
 
Registered: Jun 2007
Distribution: Fedora, RHEL, Centos
Posts: 294

Rep: Reputation: 54
how about
Quote:
ls -a /bin/[a,b,n]* >~/commands_nb.lst
 
Old 12-13-2011, 09:42 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by zQUEz View Post
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.
 
1 members found this post helpful.
Old 12-13-2011, 10:49 AM   #12
AB1826
LQ Newbie
 
Registered: Dec 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
I solved thanks to you guys!

I did: ls -a /bin | grep ^[a,n,b] > Filename

Awesome!

Thanks alot for the help!
 
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
[SOLVED] Bash Script to create directory sizes around 4GB hyperdaz Programming 20 07-29-2010 07:30 AM
Help with bash script to create directory to /home folder wegadnie Linux - General 5 10-25-2008 12:50 PM
copy and rename the existing directory using BASH script neo2k Linux - Software 3 08-20-2008 11:43 AM
webtree.bash - a script to create entire directory links in HTML mohan34u Programming 1 07-15-2007 09:07 AM


All times are GMT -5. The time now is 07:07 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