Find the answer to your Linux question:
Results 1 to 5 of 5
I'm new to scripting although I did have some programming classes a couple of years ago. I trying to make a very simple addusers bash script. I am trying to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2005
    Posts
    2

    Simple add users script from text file


    I'm new to scripting although I did have some programming classes a couple of years ago. I trying to make a very simple addusers bash script. I am trying to read from a file that has a list of usernames their fullnames. Their username and fullname are separated by a comma with the next person on a new line. I know that can get what the value I want from a command line with this: y=$(wc -l /root/user.txt | awk -F/ '{print}') . I just am unsure on how to tie this to a variable. I am getting an error of too many arguements when I run the script. Any links on where to learn more on how to do this would be very much appreciated too.

    Actually I'm having specfic trouble understanding two things, even though I figured how to make it run right.

    Why when I am using a counter do I need to use let (var) instead of just a straight equality statement to get my desired result?
    let x=[$x+1] instead of x=[$x+1]

    and what is the difference between
    y=$(wc -l /root/user.txt | awk -F/ '{print}')

    and
    y='wc -l /root/user.txt | awk -F/ '{print}''

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    and what is the difference between
    y=$(wc -l /root/user.txt | awk -F/ '{print}')

    and
    y='wc -l /root/user.txt | awk -F/ '{print}''
    do you mean `wc etc...` because `` and $() are the same thing. as to the first error, can you elaborate?

  3. #3
    Just Joined!
    Join Date
    Apr 2005
    Posts
    2
    when is use: x=[$x+1]

    the value of x would literally be 1+1

    if I use: let x=[$x+1]

    I get the desired result of it counting up

    I guess I don't see why the first way doesn't work.

  4. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044

    Re: Simple add users script from text file

    Quote Originally Posted by a_kerbouchard
    Why when I am using a counter do I need to use let (var) instead of just a straight equality statement to get my desired result?
    let x=[$x+1] instead of x=[$x+1]
    I don't use square brackets much myself, but the obvious answer is that the let version is treating x as an integer, whereas the plain "x=" version is treating it like a string. I'd have used "x = expr $x + 1" myself, but I'm kinda old-fashioned.

    and what is the difference between
    y=$(wc -l /root/user.txt | awk -F/ '{print}')

    and
    y='wc -l /root/user.txt | awk -F/ '{print}''
    Are you using backticks to surround the wc -l ... print}'? On my screen they look like ordinary single quotes. The first version is preferrable because it enables you to nest commands with having to worry about escaping nested backticks. I'm not sure why you're piping wc's output to awk, the quickest way to just get the number from wc is to use echo and stdin:

    y=$(echo $(wc -l </root/user.txt))

    This also demonstrates nested commands.
    Steve

  5. #5
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Quote Originally Posted by a_kerbouchard
    I know that can get what the value I want from a command line with this: y=$(wc -l /root/user.txt | awk -F/ '{print}') .
    What value are you trying to get ?
    Quote Originally Posted by scm
    I'd have used "x = expr $x + 1" myself, but I'm kinda old-fashioned.
    But this in a loop would be many times slower than using
    built-in arithmetic.

Posting Permissions

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