[SOLVED] bash limit the number given as first argument

Need help with C, C++, perl, python, etc?

[SOLVED] bash limit the number given as first argument

Postby arzgi » 2018-10-14 11:48

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.
arzgi
 
Posts: 389
Joined: 2008-02-21 17:03
Location: Finland

Re: bash limit the number given as first argument

Postby debiman » 2018-10-14 13:33

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.
User avatar
debiman
 
Posts: 3015
Joined: 2013-03-12 07:18

Re: bash limit the number given as first argument

Postby arzgi » 2018-10-14 14:05

$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.
arzgi
 
Posts: 389
Joined: 2008-02-21 17:03
Location: Finland

Re: bash limit the number given as first argument

Postby debiman » 2018-10-14 14:12

i'd like to help but i don't see how your last reply relates to what i wrote before that.
User avatar
debiman
 
Posts: 3015
Joined: 2013-03-12 07:18

Re: bash limit the number given as first argument

Postby arzgi » 2018-10-14 14:22

Thanks for trying, FYI (()) is arithmetic, script does not accept letters as arguments.
arzgi
 
Posts: 389
Joined: 2008-02-21 17:03
Location: Finland

Re: bash limit the number given as first argument

Postby debiman » 2018-10-14 14:23

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.
User avatar
debiman
 
Posts: 3015
Joined: 2013-03-12 07:18

Re: bash limit the number given as first argument

Postby arzgi » 2018-10-14 14:28

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$

arzgi
 
Posts: 389
Joined: 2008-02-21 17:03
Location: Finland

Re: bash limit the number given as first argument

Postby bw123 » 2018-10-14 15:04

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

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

else none the three above true
User avatar
bw123
 
Posts: 3522
Joined: 2011-05-09 06:02
Location: TN_USA

Re: bash limit the number given as first argument

Postby arzgi » 2018-10-14 15:42

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!
arzgi
 
Posts: 389
Joined: 2008-02-21 17:03
Location: Finland

Re: bash limit the number given as first argument

Postby Dai_trying » 2018-10-14 17:54

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
Dai_trying
 
Posts: 557
Joined: 2016-01-07 12:25

Re: bash limit the number given as first argument

Postby arzgi » 2018-10-15 06:41

Thanks Dai_trying, that was the right solution!
arzgi
 
Posts: 389
Joined: 2008-02-21 17:03
Location: Finland

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

Postby Dai_trying » 2018-10-15 07:26

You're welcome :)
Dai_trying
 
Posts: 557
Joined: 2016-01-07 12:25


Return to Programming

Who is online

Users browsing this forum: No registered users and 3 guests

fashionable