UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 30th, 2011, 02:52 AM
R.E.M.Addict R.E.M.Addict is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 2 R.E.M.Addict User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 57 sec
Reputation Power: 0
Writing A Shell Script in UNIX

I'm doing this lab assignment in UNIX for a I.T course I'm taking in college, I would like to know how to do these three things in the vi editor in UNIX.
1. How do you use echo command to display a message? Let's say the message is "Enter a name."
2. How do read input from the keyboard and put it into a variable? Let's say, the variable is dirname
3. How do you echo back the contents of dirname (see question 2) and create a directory by that name?

Thanks.

P.S. I'm doing this from my student directory from school on my computer at home, I'm using /homestu/rs6310

Reply With Quote
  #2  
Old September 30th, 2011, 04:15 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 48 m 25 sec
Reputation Power: 1485
Without giving too much away, you could try echo, read and mkdir. The man pages can be your friend or, if they are not installled try google.
echo will reproduce what is passed to it, to standard output which, by default, is the screen.
read will read, by default, standard input which defaults to the keyboard and populate a variable with the input.
mkdir will create a directory.

Some things to ensure you understand the basics, specfically with things like the mkdir - relative and absolute paths. Using your home directory (/homestu/rs6310) as a basis:
If you use mkdir to create a directory called test (mkdir test) you will end up (assuming you have the relevant permissions) with a directory (with a full path) called: /homestu/rs6310/test.
If you want to edit/create a file (calling it newfile) in the directory you could, using vi, use:
vi test/newfile
vi /homestu/rs6310/test/newfile
The first uses a relative path (notice the path does NOT start with a /), the second uses an absolute path (the path starts with /). Both will do the same. Now, if you were not in your home directory when you used the commands the second would still work, whilst the first one would likely fail for the reason that it would not be likely that the current directory had a sub-directory called test.
__________________
The moon on the one hand, the dawn on the other:
The moon is my sister, the dawn is my brother.
The moon on my left and the dawn on my right.
My brother, good morning: my sister, good night.
-- Hilaire Belloc

Reply With Quote
  #3  
Old September 30th, 2011, 01:00 PM
R.E.M.Addict R.E.M.Addict is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2011
Posts: 2 R.E.M.Addict User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 57 sec
Reputation Power: 0
Quote:
Originally Posted by SimonJM
Without giving too much away, you could try echo, read and mkdir. The man pages can be your friend or, if they are not installled try google.
echo will reproduce what is passed to it, to standard output which, by default, is the screen.
read will read, by default, standard input which defaults to the keyboard and populate a variable with the input.
mkdir will create a directory.

Some things to ensure you understand the basics, specfically with things like the mkdir - relative and absolute paths. Using your home directory (/homestu/rs6310) as a basis:
If you use mkdir to create a directory called test (mkdir test) you will end up (assuming you have the relevant permissions) with a directory (with a full path) called: /homestu/rs6310/test.
If you want to edit/create a file (calling it newfile) in the directory you could, using vi, use:
vi test/newfile
vi /homestu/rs6310/test/newfile
The first uses a relative path (notice the path does NOT start with a /), the second uses an absolute path (the path starts with /). Both will do the same. Now, if you were not in your home directory when you used the commands the second would still work, whilst the first one would likely fail for the reason that it would not be likely that the current directory had a sub-directory called test.


Actually, I'm already in my directory and in v editor. The name of the shell script is script6. When I'm ask to use the echo command to display the message "Enter a name" Do I type in: echo "Enter a name" or echo Enter a name. Also for read input from the keyboard and putting it into a variable called dirname, when I was going over a handout, it stated that when a shell script is run, the read command can be be used to read a line of input from the keyboard and put into variable. It also stated that the filename example would be copied from the current directory to the merlin directory.

However, I'm not copying it, so do I read the input from the keyboard and put it into a variable called dirname. I tried doing it, but got frustrated. Also what about echo back the contents of a variable. Let's say, it's dirname. Is either echo dirname or cat dirname?

Reply With Quote
  #4  
Old October 3rd, 2011, 01:10 AM
Nylex Nylex is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2005
Location: London, UK
Posts: 111 Nylex User rank is Sergeant (500 - 2000 Reputation Level)Nylex User rank is Sergeant (500 - 2000 Reputation Level)Nylex User rank is Sergeant (500 - 2000 Reputation Level)Nylex User rank is Sergeant (500 - 2000 Reputation Level)Nylex User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 14 h 11 m 7 sec
Reputation Power: 13
Quote:
Originally Posted by R.E.M.Addict
When I'm ask to use the echo command to display the message "Enter a name" Do I type in: echo "Enter a name" or echo Enter a name.


I assume you're using Bash and both work. I'm not sure whether one is preferred over the other.

Quote:
However, I'm not copying it, so do I read the input from the keyboard and put it into a variable called dirname. I tried doing it, but got frustrated. Also what about echo back the contents of a variable. Let's say, it's dirname. Is either echo dirname or cat dirname?


Show us your code for trying to read input. Neither of those ("echo dirname" or "cat dirname") will work. cat takes a filename as input (or reads from stdin) and will just output the file (so it will look for a file called "dirname"). If you know that "echo Enter a name" prints "Enter a name", then it's not hard to see that "echo dirname" is just going to print "dirname". What you need is a way to tell the shell to get the value in the variable dirname and you use $ to do this, i.e.

Code:
echo $dirname


This stuff is quite basic, so it should be in your learning materials.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Writing A Shell Script in UNIX

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap