Skip to content

Commit 19309da

Browse files
Add random_char functions
1 parent 80b08f8 commit 19309da

File tree

3 files changed

+523
-21
lines changed

3 files changed

+523
-21
lines changed

README.md

Lines changed: 244 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ To include this file in your own script, in the same directory:
2626
## Tracking
2727

2828
* Package: posix-shell-script-kit
29-
* Version: 11.0.5
29+
* Version: 11.1.0
3030
* Created: 2017-08-22T00:00:00Z
31-
* Updated: 2023-03-24T13:23:36Z
31+
* Updated: 2023-03-28T14:01:52Z
3232
* Website: https://github.com/sixarm/posix-shell-script-kit
3333
* License: GPL-2.0 or GPL-3.0 or contact us for more
3434
* Contact: Joel Parker Henderson (joel@sixarm.com)
@@ -459,7 +459,6 @@ select_character_class foo123goo456 alpha 3 1
459459
=> g
460460
```
461461
462-
463462
### reject_character_class
464463
465464
Get a string's characters that don't match a class, with optional offset and length.
@@ -487,6 +486,248 @@ reject_character_class foo123goo456 alpha 3 1
487486
=> 4
488487
```
489488
489+
## Random character helpers
490+
491+
### random_char
492+
493+
Generate random characters
494+
495+
Syntax:
496+
```sh
497+
random_char [characters [length]]
498+
```
499+
500+
Example:
501+
```sh
502+
random_char ABCDEF 8
503+
=> CBACBFDD
504+
```
505+
506+
Example hexadecimal digit uppercase:
507+
```sh
508+
random_char 0-9A-F 8
509+
=> FC56A95C
510+
```
511+
512+
Example character class for uppercase letters:
513+
```sh
514+
random_char '[:upper:]' 8
515+
=> ZMGIQBJB
516+
```
517+
518+
POSiX character classes for ASCII characters:
519+
```
520+
Class Pattern Description
521+
---------_ ------------- -----------
522+
[:upper:] [A-Z] uppercase letters
523+
[:lower:] [a-z] lowercase letters
524+
[:alpha:] [A-Za-z] uppercase letters and lowercase letters
525+
[:alnum:] [A-Za-z0-9] uppercase letters and lowercase letters and digits
526+
[:digit:] [0-9] digits
527+
[:xdigit:] [0-9A-Fa-f] hexadecimal digits
528+
[:punct:] punctuation (all graphic characters except letters and digits)
529+
[:blank:] [ \t] space and TAB characters only
530+
[:space:] [ \t\n\r\f\v] whitespace characters (space, tab, newline, return, feed, vtab)
531+
[:cntrl:] control characters
532+
[:graph:] [^ [:cntrl:]] graphic characters (all characters which have graphic representation)
533+
[:print:] [[:graph:] ] graphic characters and space
534+
```
535+
536+
### random_char_alnum
537+
538+
Get random characters using `[:alnum:]` class.
539+
540+
Syntax:
541+
```sh
542+
random_char_alnum [length]
543+
```
544+
545+
Example:
546+
```sh
547+
random_char_alnum 8
548+
=> 1Yp7M7wc
549+
```
550+
551+
### random_char_alpha
552+
553+
Get random characters using `[:alpha:]` class.
554+
555+
Syntax:
556+
```sh
557+
random_char_alnum [length]
558+
```
559+
560+
Example:
561+
```sh
562+
random_char_alpha 8
563+
=> dDSmQlYD
564+
```
565+
566+
### random_char_blank
567+
568+
Get random characters using `[:blank:]` class.
569+
570+
Syntax:
571+
```sh
572+
random_char_alnum [length]
573+
```
574+
575+
Example:
576+
```sh
577+
random_char_blank 8
578+
=> " \t \t \t"
579+
```
580+
581+
### random_char_cntrl
582+
583+
Get random characters using `[:cntrl:]` class.
584+
585+
Syntax:
586+
```sh
587+
random_char_alnum [length]
588+
```
589+
590+
Example:
591+
```sh
592+
random_char_cntrl 8
593+
=> "^c^m^r^z^a^e^p^u"
594+
```
595+
596+
### random_char_digit
597+
598+
Get random characters using `[:digit:]` class.
599+
600+
Syntax:
601+
```sh
602+
random_char_alnum [length]
603+
```
604+
605+
Example:
606+
```sh
607+
random_char_digit 8
608+
=> 36415110
609+
```
610+
611+
### random_char_graph
612+
613+
Get random characters using `[:graph:]` class.
614+
615+
Syntax:
616+
```sh
617+
random_char_alnum [length]
618+
```
619+
620+
Example:
621+
```sh
622+
random_char_graph 8
623+
=> e'2-3d+9
624+
```
625+
626+
### random_char_lower
627+
628+
Get random characters using `[:lower:]` class.
629+
630+
Syntax:
631+
```sh
632+
random_char_alnum [length]
633+
```
634+
635+
Example:
636+
```sh
637+
random_char_lower 8
638+
=> pgfqrefo
639+
```
640+
641+
### random_char_lower_digit
642+
643+
Get random characters using `[:lower:][:digit]` classes
644+
645+
Syntax:
646+
```sh
647+
random_char_alnum [length]
648+
```
649+
650+
Example:
651+
```sh
652+
random_char_lower_digit 8
653+
=> 69m7o83i
654+
```
655+
656+
### random_char_upper
657+
658+
Get random characters using `[:upper:]` class.
659+
660+
Syntax:
661+
```sh
662+
random_char_alnum [length]
663+
```
664+
665+
Example:
666+
```sh
667+
random_char_upper 8
668+
=> EGXUHNIM
669+
```
670+
671+
### random_char_upper_digit
672+
673+
Get random characters using `[:upper:][:digit:]` classes
674+
675+
Syntax:
676+
```sh
677+
random_char_alnum [length]
678+
```
679+
680+
Example:
681+
```sh
682+
random_char_upper_digit 8
683+
=> L2PT37H6
684+
```
685+
686+
### random_char_print
687+
688+
Get random characters using `[:print:]` class.
689+
690+
Syntax:
691+
```sh
692+
random_char_alnum [length]
693+
```
694+
695+
Example:
696+
```sh
697+
random_char_print 8
698+
=> ),zN87K;
699+
```
700+
701+
### random_char_space
702+
703+
Get random characters using `[:space:]` class.
704+
705+
Syntax:
706+
```sh
707+
random_char_alnum [length]
708+
```
709+
710+
Example:
711+
```sh
712+
random_char_space 8
713+
=> "\n \t\r \v \f"
714+
```
715+
716+
### random_char_xdigit
717+
718+
Get random characters using `[:xdigit:]` class.
719+
720+
Syntax:
721+
```sh
722+
random_char_alnum [length]
723+
```
724+
725+
Example:
726+
```sh
727+
random_char_xdigit 8
728+
=> eC3Ce9eD
729+
```
730+
490731
## Array helpers
491732
492733
### array_n

doc/exit-codes/index.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Exit 2 meaning usage is a widespread convention as a catch-all CLI code.
8585
EXIT_DATAERR=65
8686
```
8787

88-
The input data was incorrect in some way.
88+
The input data was incorrect in some way.
8989

9090
This should only be used for user's data and not system files.
9191

@@ -118,7 +118,7 @@ E.g. for email addresses, or remote logins, or authentication issues, etc.
118118
EXIT_NOHOST=68
119119
```
120120

121-
The host specified did not exist.
121+
The host specified did not exist.
122122

123123
E.g. for email addresses, or network requests, or web links, etc.
124124

@@ -130,7 +130,7 @@ E.g. for email addresses, or network requests, or web links, etc.
130130
EXIT_UNAVAILABLE=69
131131
```
132132

133-
A service is unavailable.
133+
A service is unavailable.
134134

135135
E.g. a support program or file does not exist. This can also be a catchall
136136
message when something does not work, but you do not know why.
@@ -142,7 +142,7 @@ message when something does not work, but you do not know why.
142142
EXIT_SOFTWARE=70
143143
```
144144

145-
An internal software error has been detected.
145+
An internal software error has been detected.
146146

147147
This should be limited to non-operating system related errors as possible.
148148

@@ -153,7 +153,7 @@ This should be limited to non-operating system related errors as possible.
153153
EXIT_OSERR=71
154154
```
155155

156-
An operating system error has been detected.
156+
An operating system error has been detected.
157157

158158
E.g. errors such as "cannot fork", "cannot create pipe", or getuid returns a
159159
user that does not exist in the passwd file, etc.
@@ -212,7 +212,7 @@ a protocol exchange.
212212
EXIT_NOPERM=77
213213
```
214214

215-
You did not have sufficient permission to perform the operation.
215+
You did not have sufficient permission to perform the operation.
216216

217217
This is not for file system problems, which use EXIT_NOINPUT or EXIT_CANTCREATE,
218218
but for higher level permissions, authorizations, etc.
@@ -419,7 +419,7 @@ EXIT_GIT_BISECT_SKIP=125
419419

420420
Git bisect: The special exit code 125 should be used when the current source
421421
code cannot be tested. If the script exits with this code, the current
422-
revision will be skipped (see git bisect skip above).
422+
revision will be skipped (see git bisect skip above).
423423

424424
Value 125 was chosen as the highest sensible value to use for this purpose,
425425
because 126 and 127 are used by shells to signal specific error status.

0 commit comments

Comments
 (0)