LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-20-2015, 04:07 PM   #1
LostinLinux1
LQ Newbie
 
Registered: Apr 2015
Posts: 1

Rep: Reputation: Disabled
Question Shell Scripts


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!
 
Old 04-20-2015, 04:30 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209
Hi and welcome to LQ.

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:

Code:
chmod +x ./myscript.sh
and then you could run it by typing:

Code:
./myscript.sh
 
Old 04-20-2015, 04:32 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,454

Rep: Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052Reputation: 2052
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.
 
Old 04-20-2015, 08:13 PM   #4
jkirchner
Member
 
Registered: Apr 2007
Location: West Virginia
Distribution: LinuxMint
Posts: 775

Rep: Reputation: 199Reputation: 199
If you get stuck, post what you have tried so far (use code tags) and we can comment from there.
 
Old 04-20-2015, 08:51 PM   #5
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 1,205

Rep: Reputation: 197Reputation: 197
Quote:
Originally Posted by suicidaleggroll View Post
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.
 
Old 04-20-2015, 09:01 PM   #6
kmhuntly
Member
 
Registered: Mar 2015
Location: Cheektowaga, NY
Distribution: ArchLinux
Posts: 34

Rep: Reputation: 7
Shell Scripts

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
 
Old 04-21-2015, 09:49 AM   #7
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 2,974
Blog Entries: 1

Rep: Reputation: 620Reputation: 620Reputation: 620Reputation: 620Reputation: 620Reputation: 620
$ nano homework.sh

Code:
#!/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.
 
1 members found this post helpful.
Old 04-21-2015, 01:09 PM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,119
Blog Entries: 10

Rep: Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815Reputation: 1815
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.
 
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
How do I make these shell scripts with ash as my shell? riahc3 Programming 13 08-15-2012 03:57 AM
best way to automate shell commands - shell script or python scripts or something els parangsaraf Linux - Newbie 11 08-08-2012 06:17 PM
shell scripts jordan66 Linux - Newbie 5 12-01-2011 01:17 PM
[SOLVED] shell scripts Ramesh Programming 2 11-02-2010 11:49 AM
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM


All times are GMT -5. The time now is 07:52 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration