Can I Have Some Help Writing This Bash Script Since Is My First Time Doing One?
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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
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 ...
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 ?
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
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'.
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'.
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)
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.