#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    2
    Rep 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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,446
    Rep Power
    1751
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    2
    Rep Power
    0
    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?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2005
    Location
    London, UK
    Posts
    111
    Rep Power
    15
    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.

    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.

IMN logo majestic logo threadwatch logo seochat tools logo