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.
I am taking a Linux class and am doing horribly, I just do not understand Linux. Can anyone please tell me how to create a shell script containing the `echo`, `whoami`, `groups`, and `pwd` commands?
I can do these as individual commands but cannot figure out how to do this in a shell script and get the output to produce output similar to the following:
You are:
root
You are a member of the following groups:
root bin daemon sys adm disk wheel
Your current working directory is:
/tmp
My hair is getting thinner and grayer because of this.. Please help!
Please make sure you're clear what each of those commands does.
Start your script with the following (if you're in bash):
Code:
#!/usr/bin/bash
What does the 'echo' command do? Try to write a small script just using the 'each' command. Once you're crystal clear how it works, you might want to expand it with other commands that you mentioned.
One more thing that you need to remember is that you need to give the script executable rights in order to run it. For example:
A script is nothing more than a text file that contains a set of commands. If you can run the individual commands to get the output you want, then just put those exact same commands into a text file, give it execute permissions, and you have a script.
A script is nothing more than a text file that contains a set of commands. If you can run the individual commands to get the output you want, then just put those exact same commands into a text file, give it execute permissions, and you have a script.
Agree with this one. Make like simple just think first that a script contains a sets of commands which is group in one file called script.
Like:
you="$USER"
echo "You are: $you"
usrgroup=groups "$USER"
echo "You are member of the following groups: $usrgroup"
You can run each command individually, but when you group them it's a script already.
Open vi or any editor you like and group the commands and save it with any file name with ".sh" extension.
as well as the above, I'd recommend poking around github or google code or anywhere else that you can find public source code repositories to look at real world examples. it doesn't make a difference if you understand it completely when you start, because at the end you will
#!/bin/bash
VAR_WHOAMI=$(whoami)
VAR_GROUPS=$(groups)
VAR_PWD=$(pwd)
echo "You are:"
echo $VAR_WHOAMI
echo "You are a member of the following groups:"
echo $VAR_GROUPS
echo "Your current working directory is:"
echo $VAR_PWD
echo "My homework is done."
exit 0
$ chmod +x homework.sh
$ ./homework.sh
Where $() is the modern equivalent of ``.
Where $VAR returns the value of VAR.
Of course you could forgo the variables and just have the commands instead of echo $VAR. You don't even need the `` or $() if you go that route. Although not a very good script as it doesn't check if those commands exist before trying to execute them. Or take any action if they don't exist.
There are links for BASH programming in my signature. Review those. As said a BASH program is really a text file organized in a syntactically correct fashion. So learn the basics, the most fundamental scripts, which are examples and exercises in the documentation. When you start making your own scripts and have specific questions, you can post your scripts with detailed questions.
As I always say, "Whatever you can type on a command line, you can script." So therefore when you say that you can do those commands, then you can likewise learn how to employ those commands in a script. Recommend you check out my BASH blog entry, it shows information about how to debug a BASH script and offers other tips.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.