Solaris scripting problem with ksh88


 
Thread Tools Search this Thread
Operating Systems Solaris Solaris scripting problem with ksh88
# 1  
Old 09-10-2014
Solaris scripting problem with ksh88

Hello,

I want to pick a random element from a list, and created these 2 lines, which work very well in ksh93. Unfortunately, I get this "bad substitution" message in ksh88.

I'm wondering if there's an equivalent to the second line of my script.. or if I have to install ksh93 to make this work.

Here are my lines:

Code:
MATRICE="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@;:[].{}+=<>#|()^&_0123456789"

PASSWD="${MATRICE:$(($RANDOM%${#MATRICE})):1}"

Thanks in advance
# 2  
Old 09-10-2014
You may need to split up the logic to achieve this, or awk may do this for you. I did something very clumsy many years ago with this:-
Code:
echo "$arg"|awk '{print substr($1,1,1)}'`

Perhaps that can be adjusted to something more suitable. My stab is:-
Code:
((rand=$RANDOM%7))      # Generates number from 0 to 6
((rand=$rand+1))        # To get number from 1 to 7

PASSWD=`echo $MATRICE $rand|awk '{print substr($1,$2,1)}'`

..... but I'm not saying that this is anywhere near the best Smilie




I hope that this helps though,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 09-10-2014
Not sure if this works in ksh88 as I can't test it:
Code:
echo $((RANDOM%${#MATRICE})) | awk -vM="$MATRICE" 'BEGIN {n=split (M, T, "")} {print T[$1]}'

And, it might not be the most efficient approach if you want to use it for e.g. lengthy passwords...

EDIT: Try
Code:
PWLEN=25
while ((++I < PWLEN)); do echo $((RANDOM%${#MATRICE})); done | awk -vM="$MATRICE" 'BEGIN {n=split (M, T, "")} {printf "%s", T[$1]} END {printf "\n"}'
Z7{6M5<2Z6Z6691gcq[k0Z6p

# 4  
Old 09-10-2014
Quote:
Originally Posted by rbatte1
You may need to split up the logic to achieve this, or awk may do this for you. I did something very clumsy many years ago with this:-
Code:
echo "$arg"|awk '{print substr($1,1,1)}'`

Perhaps that can be adjusted to something more suitable. My stab is:-
Code:
((rand=$RANDOM%7))      # Generates number from 0 to 6
((rand=$rand+1))        # To get number from 1 to 7

PASSWD=`echo $MATRICE $rand|awk '{print substr($1,$2,1)}'`

..... but I'm not saying that this is anywhere near the best Smilie


I hope that this helps though,
Robin
Thanks!! This works great, and I certainly don't mind if it's not very efficient Smilie


Quote:
Originally Posted by RudiC
Not sure if this works in ksh88 as I can't test it:
Code:
echo $((RANDOM%${#MATRICE})) | awk -vM="$MATRICE" 'BEGIN {n=split (M, T, "")} {print T[$1]}'

And, it might not be the most efficient approach if you want to use it for e.g. lengthy passwords...

EDIT: Try
Code:
PWLEN=25
while ((++I < PWLEN)); do echo $((RANDOM%${#MATRICE})); done | awk -vM="$MATRICE" 'BEGIN {n=split (M, T, "")} {printf "%s", T[$1]} END {printf "\n"}'
Z7{6M5<2Z6Z6691gcq[k0Z6p

I tried the edited code, but it's not working. Do I need to initialize I=0?
Code:
MATRICE="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@;:[].{}+=<>#|()^&_0123456789"
I=0
PWLEN=25
while ((++I < $PWLEN)); do echo $((RANDOM%${#MATRICE})); done | awk  -vM="$MATRICE" 'BEGIN {n=split (M, T, "")} {printf "%s", T[$1]} END  {printf "\n"}'

# 5  
Old 09-10-2014
What's the output of that while loop with the pipe to awk removed?
Code:
unset I
while ((++I < PWLEN)); do echo $((RANDOM%${#MATRICE})); done
48
86
63
75
46
3
74
43
87
44
62
83
3
43
39
82
18
49
89
13
14
5
90
8

# 6  
Old 09-10-2014
It works properly if I run the commands manually or if I put the commands in a file:
Code:
./test.ksh
9
31
25
38
72
91
81
52
51

If I add "#!/bin/ksh" at the beginning of my script, I get this error:
Code:
./test.ksh
./test.ksh[5]: ++I < 10: bad number

Here's the inside of the script
Code:
#!/bin/ksh
MATRICE="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@;:[].{}+=<>#|()^&_0123456789"
unset I
PWLEN=10
while ((++I < $PWLEN)); do echo $((RANDOM%${#MATRICE})); done


Last edited by rbatte1; 09-10-2014 at 12:17 PM.. Reason: Added CODE tags
# 7  
Old 09-10-2014
I would turn MATRICE into an array with one character per array element. Then you just pick a random index into the array. This will work with all versions of ksh and it will be very fast.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. OS X (Apple)

FFT for the AMIGA through ksh88 shell.

I don't know if anyone is interested but I have been meddling with FFT for the AMIGA. (Sadly we AMIGAns don't have these luxuries through any scripting language. Below is a Python snippet that uses the builtin 'cmath' module to work with the lowly Python 2.0.1 for the AMIGA. It is part of a... (0 Replies)
Discussion started by: wisecracker
0 Replies

2. Shell Programming and Scripting

FTP script in ksh88

Hi I tried the following code to FTP the files from test server to dev #!/bin/ksh DST=/home/files cd $DST ftp -inv 'test_serv101' << EOF quote USER test quote PASS test # File Path on test server cd /etc/home/Or_Files ascii mget curMonth* $DST quit EOF when i try the above code it... (4 Replies)
Discussion started by: smile689
4 Replies

3. Shell Programming and Scripting

Comparing Strings in ksh88

Hi I tried the following string comparison script in Ksh88 #!/bin/ksh str1='aC' str2='ABC' if then echo "Equal" else echo "Not Equal" fi Though str1 and str2 are not equal the script output says Equal . Please correct me Thanks (2 Replies)
Discussion started by: smile689
2 Replies

4. Shell Programming and Scripting

[ksh88 and awk] Number of fields with a value.

Hi, With: # VALUES="one~two~~~" # echo $VALUES | awk 'BEGIN {FS="~"} {print NF}' 5 I can determine the number of fields. How to determine the number of fields with a value ? In this case 2. Thanks in advance, ejdv (6 Replies)
Discussion started by: ejdv
6 Replies

5. Shell Programming and Scripting

namerefs alternative for KSH88

I have to use KSH88, so going to BASH, perl etc. is not an option. Below is a much simplified verison of what I am doing (aka ignore my cut command and i not increasing) :) i=1 BIGSTRING="one two three four five six seven eight" while ]; do typeset "STRING$i=`echo $BIGSTRING| cut -d' '... (8 Replies)
Discussion started by: nitrobass24
8 Replies

6. Shell Programming and Scripting

Download AT&T ksh88 ?

Hello, I need ksh88 for my linux system - and I don't want pdksh. Possible to get original ksh 88 binaries or source ? (I don't need ksh93 which is available) thanks Vilius (1 Reply)
Discussion started by: vilius
1 Replies

7. Shell Programming and Scripting

Substring in ksh88 ?

Hello, ksh88 doesn't support ${var:x:y}. Any alternatives to get substring ? thanks Vilius (2 Replies)
Discussion started by: vilius
2 Replies

8. Shell Programming and Scripting

ksh88 or ksh93

Hi all! Does anybody know how can I check if any UNIX installation has implemented ksh88 or ksh93? Thanks in advance. Néstor. (3 Replies)
Discussion started by: Nestor
3 Replies

9. Shell Programming and Scripting

ksh88 - curses

I was wondering if there is anyway to use the curses library with ksh88. I saw Shell Curses function library which says I can use /usr/local/functions/shellcurses on ksh93 but I am on ksh88. I am on a HP-UX box. (0 Replies)
Discussion started by: IMTheNachoMan
0 Replies

10. Solaris

Problem in for loop of shell scripting in solaris

Hi below is my script for((i=0;i<=$TOTAL;i++)) do echo "IP's created are $s1.$s2.$s3.$s4" s4=`expr $s4 + 1` done where s1,2,3,4 are input varibles below error occurs while running the script syntax error at lin 11: '(' unexpected ... (12 Replies)
Discussion started by: krevathi1912
12 Replies
Login or Register to Ask a Question