LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 04-30-2008, 08:15 AM   #1
fiona79us
LQ Newbie
 
Registered: Apr 2008
Posts: 3

Rep: Reputation: 0
Can I Have Some Help Writing This Bash Script Since Is My First Time Doing One?


Write a script that extracs all the usersids from the /home directory
into a file along the the last login date.

NAME OF SCRIPT: usage.bas

EXAMPLE:
./usage.bas
 
Old 04-30-2008, 08:26 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
no sorry, we can't help you from no starting point at all, that's called cheating. how far have you got of your own accord? ask us some specific question and we can give some specific advice, but we can't just write a script for you.
 
Old 04-30-2008, 09:54 AM   #3
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
First, you need to have an interpreter specified by your script. This is what the first line of the script does. That line begins with a #! to signify that it is to execute what follows as the interpreter. I suggest you use /bin/sh, but you can use whatever shell you like.

After that, you need to figure out what commands get usernames from /home and what commands get last login information. You will also need to determine what commands will help break up a many line output into subsets.

Study:

ls
last
head
awk

Notes:
Variables can be assigned by
VARIABLE=text
VARIABLE="Text with spaces"
VARIABLE=`command output`

You will need a for loop

for VARIABLE in `command output` ; do
...
...
...
done

If you get stuck, ask.

HTH

Forrest
 
Old 04-30-2008, 01:33 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Here's a challenge: Write a script that automatically detects when someone does a cut and paste from the instructor's assignment sheet.

Seriously---to get help with homework, you should:
--acknowledge that it is homework
--show what you have already tried
--mention what textbook(s) you are using
--ask a question in your own words
 
Old 04-30-2008, 06:21 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Actually, I'd recommend using
#!/bin/bash
for the 1st line. /bin/sh MAY be symlinked to /bin/bash, but it's not required, and in which case it'll actually be the POSIX sh shell ... which is not the same thing ...
 
Old 05-01-2008, 03:26 AM   #6
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
I think it's best to use '#!/bin/sh' because whatever it is linked to must be Bourne-shell compatible. What if you use ksh and don't even have bash installed ?
 
Old 05-01-2008, 09:13 AM   #7
fiona79us
LQ Newbie
 
Registered: Apr 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Well hello everyone, i think we started off on the wrong foot here, so let me start again...
I am not asking for anyone to solve this for me , i only need a guide on how to start. i have some small knowledge of linux but not on bash as much. so far i know that i have to use maybe ls -l/home/, i also know that i have to create a file and put all the users in there, i will start the script with

#!/bin/bash
#
 
Old 05-01-2008, 10:21 AM   #8
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Remember to read man pages, for example 'man last'.

'head' will get the beginning of a file, for example 'head -n 1' will get the first line of a file. You must use the pipe '|' to pipe program output from one to another.

For example try this:
Code:
last | head -n 2 | tail -n 1
that should give a good start on things (maybe even solve everything).

Oh, and to redirect output into a file you can use the overwrite/create '>' or append '>>'. For example 'last | head -n 2 | tail -n 1 > /home/$USER/testing-script'.

Here are some good guides, study them well:
http://www.grymoire.com/Unix/
http://tldp.org/LDP/abs/html/

EDIT: It might take you a while to figure awk out, so I'll give you a hint ... piping to
Code:
awk '{ print $2 }'
will print the second tabbed entry of program output stream.

That's all I can say, if you put all that together, it's more than enough to figure it out. Remember, read the guides.

Last edited by H_TeXMeX_H; 05-01-2008 at 10:25 AM.
 
Old 05-01-2008, 04:45 PM   #9
fiona79us
LQ Newbie
 
Registered: Apr 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by H_TeXMeX_H View Post
Remember to read man pages, for example 'man last'.

'head' will get the beginning of a file, for example 'head -n 1' will get the first line of a file. You must use the pipe '|' to pipe program output from one to another.

For example try this:
Code:
last | head -n 2 | tail -n 1
that should give a good start on things (maybe even solve everything).

Oh, and to redirect output into a file you can use the overwrite/create '>' or append '>>'. For example 'last | head -n 2 | tail -n 1 > /home/$USER/testing-script'.

Here are some good guides, study them well:
http://www.grymoire.com/Unix/
http://tldp.org/LDP/abs/html/

EDIT: It might take you a while to figure awk out, so I'll give you a hint ... piping to
Code:
awk '{ print $2 }'
will print the second tabbed entry of program output stream.

That's all I can say, if you put all that together, it's more than enough to figure it out. Remember, read the guides.
thank you

i'm gonna try and let you know the outcome

Fiona
 
Old 05-01-2008, 06:00 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
More useful links:
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://rute.2038bug.com/index.html.gz

Let us know if you get stuck

Last edited by chrism01; 05-01-2008 at 06:05 PM.
 
Old 05-01-2008, 06:05 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
H_TeXMeX_H, can you prove that 'it's best to use '#!/bin/sh' because whatever it is linked to must be Bourne-shell compatible.' ?
AFAIK, it's like I said, it MAY link to bash (or soemthing else) or it's the POSIX sh, but there's no rule saying which or whether what it points to has to be compatible with anything else.
If you can show me link proving otherwise I'd be glad of the update. (seriously)
 
Old 05-01-2008, 06:27 PM   #12
forrestt
Senior Member
 
Registered: Mar 2004
Location: Cary, NC, USA
Distribution: Fedora, Kubuntu, RedHat, CentOS, SuSe
Posts: 1,288

Rep: Reputation: 99
chrism01, I think the point he is making (which is the same reason I said /bin/sh) is because you will always have /bin/sh on your system. You can't guarantee you will have bash.

Forrest
 
Old 05-02-2008, 10:53 AM   #13
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Quote:
Originally Posted by forrestt View Post
chrism01, I think the point he is making (which is the same reason I said /bin/sh) is because you will always have /bin/sh on your system. You can't guarantee you will have bash.

Forrest
Yes, indeed.

What you must understand is that '/bin/sh' will not exist as a real file, it will always be a symlink on modern systems. It will be a symlink to a Bourne compatible shell such as bash or ksh. You can use '/bin/bash' without any problems on most systems. Also, sh and bash are never installed simultaneously on one system, just bash is installed. If sh is installed, the system is ancient and the script will fail in most cases anyway.

To try to summarize it:
The greater concern here is not that sh will exist as the original Bourne shell, it will not. The greater concern is whether bash will exist or will it have been replaced by ksh or some other shell.

I have no real links to veriy whether my way is better, but I think it's better. You can do whichever you think is better. I always use '/bin/sh' because I know this is never the old sh Bourne shell, it's always bash or ksh.
 
  


Reply



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
Help Me>> Need help in writing Bash script lamak_98 Programming 6 10-04-2007 10:44 AM
Writing a bash script. lebabyg Linux - General 2 03-31-2007 11:39 AM
Bash (help writing script) lebabyg Linux - General 7 07-04-2006 05:22 PM
writing a bash script poiuytrewq Linux - Newbie 2 10-07-2004 10:26 PM
writing bash script ankitgdit Programming 4 08-19-2003 06:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:58 PM.

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
Open Source Consulting | Domain Registration