Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Bash script problem...

  1. #1
    Join Date
    Jun 2008
    Location
    Dublin, Ireland
    Posts
    298

    Default Bash script problem...

    Hi,

    I am trying to output the contents of PASS below to a file but the file is always blank even though PASS has a value... I have tried everything. It always comes out blank. You run the script as follows:

    Code:
    sh scriptname.sh Tom Thumb
    Here is the script:

    Code:
    #!/bin/sh
    
    FIRSTNAME="$(echo "$1" | tr [a-z] [A-Z])"
    SURNAME="$(echo "$2" | tr [a-z] [A-Z])"
    CLIENT=$SURNAME"_"$FIRSTNAME
    PASS=</dev/urandom tr -dc A-Za-z0-9| (head -c $1 > /dev/null 2>&1 || head -c 8)
    FILE="$CLIENT.txt"
    
    # create password file
    touch $FILE
    
    # write password to file
    
    echo ${PASS} > ${FILE}
    $PASS definitely has a value. I have echoed it to the screen... any help would be great. This is driving me mad!

    /jlar

  2. #2
    ab@novell.com NNTP User

    Default Re: Bash script problem...

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Running your command which sets '$PASS' I get no value. Are you sure
    that is working properly? Perhaps you should echo /dev/urandom and then
    run the other stuff against data there.

    Good luck.





    eeijlar wrote:
    > Hi,
    >
    > I am trying to output the contents of PASS below to a file but the file
    > is always blank even though PASS has a value... I have tried everything.
    > It always comes out blank. You run the script as follows:
    >
    >
    > Code:
    > --------------------
    > sh scriptname.sh Tom Thumb
    > --------------------
    >
    >
    > Here is the script:
    >
    >
    > Code:
    > --------------------
    >
    > #!/bin/sh
    >
    > FIRSTNAME="$(echo "$1" | tr [a-z] [A-Z])"
    > SURNAME="$(echo "$2" | tr [a-z] [A-Z])"
    > CLIENT=$SURNAME"_"$FIRSTNAME
    > PASS=</dev/urandom tr -dc A-Za-z0-9| (head -c $1 > /dev/null 2>&1 || head -c 8)
    > FILE="$CLIENT.txt"
    >
    > # create password file
    > touch $FILE
    >
    > # write password to file
    >
    > echo ${PASS} > ${FILE}
    > --------------------
    >
    >
    > $PASS definitely has a value. I have echoed it to the screen... any
    > help would be great. This is driving me mad!
    >
    > /jlar
    >
    >

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD4DBQFI4k+d3s42bA80+9kRArWSAJ4rqNlWrrcn0vwEvUCMrYb0tbvIigCYztwb
    xxf4uzpdeePHI0jFMeieKA==
    =AVTi
    -----END PGP SIGNATURE-----

  3. #3
    Join Date
    Jun 2008
    Location
    Dublin, Ireland
    Posts
    298

    Post Re: Bash script problem...

    Hi

    Thanks for your reply... I think it has a value...

    It echoes to the screen, if you run this instead:

    Code:
    FIRSTNAME="$(echo "$1" | tr [a-z] [A-Z])"
    SURNAME="$(echo "$2" | tr [a-z] [A-Z])"
    CLIENT=$SURNAME"_"$FIRSTNAME
    PASS=</dev/urandom tr -dc A-Za-z0-9| (head -c $1 > /dev/null 2>&1 || head -c 8)
    FILE="$CLIENT.txt"
    
    # create password file
    touch $FILE
    
    echo $PASS
    # write password to file
    
    echo ${PASS} > ${FILE}
    not to a file though... I need it to be a variable as I need it in later in the script. I only posted the bit I couldn't get working here..

  4. #4
    ab@novell.com NNTP User

    Default Re: Bash script problem...

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    I still can't make it work... but the following works just fine (all on
    one line):

    PASS=`cat /dev/urandom |tr -dc A-Za-z0-9| (head -c $1 > /dev/null 2>&1
    || head -c 8)`

    Good luck.





    eeijlar wrote:
    > Hi
    >
    > Thanks for your reply... I think it has a value...
    >
    > It echoes to the screen, if you run this instead:
    >
    >
    > Code:
    > --------------------
    > FIRSTNAME="$(echo "$1" | tr [a-z] [A-Z])"
    > SURNAME="$(echo "$2" | tr [a-z] [A-Z])"
    > CLIENT=$SURNAME"_"$FIRSTNAME
    > PASS=</dev/urandom tr -dc A-Za-z0-9| (head -c $1 > /dev/null 2>&1 || head -c 8)
    > FILE="$CLIENT.txt"
    >
    > # create password file
    > touch $FILE
    >
    > echo $PASS
    > # write password to file
    >
    > echo ${PASS} > ${FILE}
    > --------------------
    >
    >
    > not to a file though... I need it to be a variable as I need it in
    > later in the script. I only posted the bit I couldn't get working here..
    >
    >

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.2 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQFI4lcN3s42bA80+9kRAjyqAJ9seLNTT4xE8EGPwOWQhqV2SjpZxACfRV2F
    sBPPvui6/G7j3Dzw6YCIoKE=
    =rWqc
    -----END PGP SIGNATURE-----

  5. #5
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Bash script problem...

    Code:
    PASS=</dev/urandom tr -dc A-Za-z0-9| (head -c $1 > /dev/null 2>&1 || head -c 8)
    This is just gibberish shell code. And I'm being charitable.

  6. #6
    Join Date
    Jun 2008
    Location
    Dublin, Ireland
    Posts
    298

    Default Re: Bash script problem...

    Perhaps... but you should see the other scripts to generate random numbers... 500 lines. This seemed the simplest which I'm always in favour of

  7. #7
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Bash script problem...

    The problem is your code doesn't even do what you think it does. A 500 line program that works is still better than a 1-liner that doesn't. Not that I think this task needs 500 lines.

  8. #8
    Join Date
    Jun 2008
    Location
    Dublin, Ireland
    Posts
    298

    Default Re: Bash script problem...

    Hi ken_yap,

    I am used to Perl and Shell scripting seems very limited (at least I think so!) after using Perl. I found a Perl script that does the trick... I am sure you can do the same in Shell.

    Thanks for your help...

    /jlar

    Code:
    sub randomPassword {
     my $password;
     my $_rand;
    
     my $password_length = $_[0];
     if (!$password_length) {
      $password_length = 10;
     }
    
     my @chars = split(" ",
     "a b c d e f g h i j k l m n o
      p q r s t u v w x y z A B C D
      E F G H I J K L M N O P Q R S
      T U V W X Y Z 0 1 2 3 4 5 6 7 8 9");
    
     srand;
    
     for (my $i=0; $i <= $password_length ;$i++) {
      $_rand = int(rand 71);
      $password .= $chars[$_rand];
     }
     return $password;
    }

  9. #9
    Join Date
    Sep 2008
    Location
    Bangalore, India
    Posts
    70

    Default Re: Bash script problem...

    The problem in your code is the line where you compute PASS.
    In your script it is
    Code:
    PASS=</dev/urandom tr -dc A-Za-z0-9| (head -c $1 > /dev/null 2>&1 || head -c 8)
    It should be
    Code:
    PASS=`</dev/urandom tr -dc A-Za-z0-9| (head -c $1 > /dev/null 2>&1 || head -c 8)`
    Notice the back quotes around the command. Modify this line and your shell script should work fine. Hope that helps.

    Shell scripting is very powerful.You can find more tips and tricks here
    Thejaswi Raya: Shell Scripting

    - Thejaswi Raya

  10. #10
    Join Date
    Sep 2008
    Location
    Bangalore, India
    Posts
    70

    Default Re: Bash script problem...

    Quote Originally Posted by eeijlar View Post
    Hi,

    I am trying to output the contents of PASS below to a file but the file is always blank even though PASS has a value... I have tried everything. It always comes out blank. You run the script as follows:

    Code:
    sh scriptname.sh Tom Thumb
    Here is the script:

    Code:
    #!/bin/sh
    
    FIRSTNAME="$(echo "$1" | tr [a-z] [A-Z])"
    SURNAME="$(echo "$2" | tr [a-z] [A-Z])"
    CLIENT=$SURNAME"_"$FIRSTNAME
    PASS=</dev/urandom tr -dc A-Za-z0-9| (head -c $1 > /dev/null 2>&1 || head -c 8)
    FILE="$CLIENT.txt"
    
    # create password file
    touch $FILE
    
    # write password to file
    
    echo ${PASS} > ${FILE}
    $PASS definitely has a value. I have echoed it to the screen... any help would be great. This is driving me mad!

    /jlar
    In addition to my previous post, you can also use this piece of code to compute PASS
    Code:
    PASS=`cat /dev/urandom | tr -dc a-zA-Z0-9 | head -c 8`
    This I feel is more readable and you really dont need "head -c $1 > /dev/null 2>&1". Just a suggestion.

    - Thejaswi Raya

Page 1 of 2 12 LastLast

Posting Permissions

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