Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[SOLVED] bash limit the number given as first argument

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

[SOLVED] bash limit the number given as first argument

#1 Post by arzgi »

Script does what I meant it to do except last else chunk. If I comment it out, script works for numbers 1-32 as the first argument, and just exits, if given number is higher. Idea is to limit output to 32 characters, but if I enable that else clause, it will take and print any positive number of characters. Why so?

Code: Select all

if  [ $# -eq 0 ]; then
     < /dev/urandom tr -dc [:graph:] | head -c${1:-8};echo;
     exit 0
elif (( $1 < 1 )); then
        exit 1
elif (( $1 < 33 )); then
    < /dev/urandom tr -dc [:graph:] | head -c${1:-$1};echo;
    exit 0
else 
    < /dev/urandom tr -dc [:graph:] | head -c${1:-32};echo;
    exit 0
fi
Last edited by arzgi on 2018-10-15 06:41, edited 1 time in total.

User avatar
debiman
Posts: 3063
Joined: 2013-03-12 07:18

Re: bash limit the number given as first argument

#2 Post by debiman »

what does this do?
please break it down (for us) step by step.

i can see there's at least one error here, or at least this doesn't make any sense:

Code: Select all

${1:-$1}
if $1 is undefined, fill it with $1???

also keep in mind that bash doesn't differentiate between strings and numbers. it does not necessarily return an error if you use a string in an arithmetic operation.

arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: bash limit the number given as first argument

#3 Post by arzgi »

$1 is the first argument given to bash script, there are nine of them $1-$9. It works, I can give any number between 1-32, and it outputs as many characters. Thank's for reply!

Okay, step by step:

Code: Select all

if  [ $# -eq 0 ]; then
     < /dev/urandom tr -dc [:graph:] | head -c${1:-8};echo;
     exit 0
This checks the number of given arguments, if none, it uses default, which I set to 8 character random string.

Code: Select all

elif (( $1 < 1 )); then
        exit 1
I think this is quite self explanatory, password which is 0 bytes lenght, is not very useable.

Code: Select all

elif (( $1 < 33 )); then
    < /dev/urandom tr -dc [:graph:] | head -c${1:-$1};echo;
    exit 0
And this, as I explained, generates random string as long as the first argument between 1-32.

Code: Select all

else 
    < /dev/urandom tr -dc [:graph:] | head -c${1:-32};echo;
    exit 0
fi
This was meant to restrict output to 32 characters even if bigger number is given as first argument. But does not work, and I do not understand why.
Last edited by arzgi on 2018-10-14 14:19, edited 1 time in total.

User avatar
debiman
Posts: 3063
Joined: 2013-03-12 07:18

Re: bash limit the number given as first argument

#4 Post by debiman »

i'd like to help but i don't see how your last reply relates to what i wrote before that.

arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: bash limit the number given as first argument

#5 Post by arzgi »

Thanks for trying, FYI (()) is arithmetic, script does not accept letters as arguments.

User avatar
debiman
Posts: 3063
Joined: 2013-03-12 07:18

Re: bash limit the number given as first argument

#6 Post by debiman »

arzgi wrote:script does not accept letters as arguments.
so you posted only part of the script?
because i do not see that safety implemented anywhere.
anyhow it still leaves the question what the script does and what the desired outcome is etc.

arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: bash limit the number given as first argument

#7 Post by arzgi »

No, that's all there is, well the first line was missing, here:

Code: Select all

#!/bin/bash
(()) is for integer calculation, thats why it just exits, if I try to give letter as argument.

And sample run:

Code: Select all

arto@samsung:bin$ salasana.sh 
X/h=l!xl
arto@samsung:bin$ salasana.sh a
arto@samsung:bin$ salasana.sh aabba
arto@samsung:bin$ salasana.sh 15
YObP`Y,~Y/8*Q?H
arto@samsung:bin$ salasana.sh 60
C<#o_nmEd/?abn^xC|YnJQRA->VsfEBL16}^b5clfd~%VDF4i-y5IP4cl6tU
arto@samsung:bin$ 


User avatar
bw123
Posts: 4015
Joined: 2011-05-09 06:02
Has thanked: 1 time
Been thanked: 28 times

Re: bash limit the number given as first argument

#8 Post by bw123 »

only one can be true either the if/elif block or the else?

if...
or elif ...
or elif...

else none the three above true
resigned by AI ChatGPT

arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: bash limit the number given as first argument

#9 Post by arzgi »

1. no arguments, default value 8.
2. argument less than one, too short, rejected.
3. check if the first argument if 1-32, print as long random string.
4. all, others, I checked, and IMHO this all integers above 32. In that case, use only 32 first characters.

Thanks for input!

Dai_trying
Posts: 1100
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: bash limit the number given as first argument

#10 Post by Dai_trying »

I think this will work for you.

Code: Select all

#!/bin/bash

if  [ $# -eq 0 ]; then
    < /dev/urandom tr -dc [:graph:] | head -c${1:-8};echo;
    exit 0
elif (( $1 < 1 )); then
    exit 1
elif (( $1 < 33 )); then
    < /dev/urandom tr -dc [:graph:] | head -c${1:-$1};echo;
    exit 0
else
    < /dev/urandom tr -dc [:graph:] | head -c32;echo;
    exit 0
fi

arzgi
Posts: 1185
Joined: 2008-02-21 17:03
Location: Finland
Been thanked: 31 times

Re: bash limit the number given as first argument

#11 Post by arzgi »

Thanks Dai_trying, that was the right solution!

Dai_trying
Posts: 1100
Joined: 2016-01-07 12:25
Has thanked: 5 times
Been thanked: 16 times

Re: [SOLVED] bash limit the number given as first argument

#12 Post by Dai_trying »

You're welcome :)

Post Reply