Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2011
    West Jefferson, NC

    Question New to scripting. HELP PLEASE!!!

    Hello everyone.

    I have been playing with Linux/Unix for about a year now and recently in my 2nd semester of Network Engineering we have started basic scripting.
    I am totally lost.

    We have no book in our class, and the instructor will give us very simple (simple to you guys) examples of how to use the "sed" or "tr" commands and then he gives us an assignment to do and dismisses class.

    Once I get the script started, I can stumble my way through it enough to make it work, but I am not really understanding what I should be doing and why.

    For Example, this is my current homework assignment:

    Review of tr command.

    Create a script to create unix accounts from the /home/share/account.dat file. The accounts should be at most 8 characters long, using lower case letters, and created from the last name + the first initial. If the last name is 8 characters or longer it should be up to the 8 character of the last name. The password for the accounts should be created by using the employee status followed by the first and last initals and the last 4 digits of the social security number. No duplicate account may exist.

    368613610 MOFFETT, IVY FT

    Would become

    Account moffetti has a password of fim3610.

    Where do I even begin with this? Please help, I am so lost.

  2. #2
    Linux User
    Join Date
    Nov 2008
    Tokyo, Japan
    Good question.
    The "sed" command uses the infamous and highly essoteric "regex". "Regex" is short for "Regular Expression", which is essentially a very simple language for doing find-replace operations on strings. "Sed" is short for "stream edit" because you can pipe a file to "sed" and sed will execute the search and replace command once for every line in the input file.

    Do a quick google search for "Linux sed regex examples". That would be my starting point.

    The "tr" (short for "translate") command can be used to convert input to all lower-case. Check the "tr" manual page. For good measure, you can pipe information to "sort -u" to make sure all entries are unique, but I would rather the system fail with an error message if it tries to create two users of the same name.

    If you don't know what I mean by "pipe", you should ask for tutoring from your teacher. But to give you a hint, you can pipe files through "sed", and replace each input with a "useradd" command:
    sed -e 's/<regex-pattern-match>/useradd \1/' </home/share/account.dat | tr <switches> >output.file
    source output.file
    So if you can follow the above code, I think that should give you enough of a head-start. I of course leave the <regex-pattern-match> to the "sed" command and the <switches> to the "tr" command for you to investigate on your own (it is homework after all!). Also, the above code where the bit of text that says "/useradd \1/" is also incomplete, but it I would be doing your homework for you if I told you what should go there.

    Let us know how it goes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts