A very weird problem about getting a random string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A very weird problem about getting a random string
# 1  
Old 06-26-2013
Question A very weird problem about getting a random string

Hi, guys
Here is my script and it's an imaginary script to recast a string randomly.
Code:
#!/bin/bash
# scriptname: recast_string
# purpose: recast a string randomly like this "abc123" --> "21a3cb" or "a31b2c"

function recast()
{
        local original_string=$1
        local changed_string=${original_string}
        local recast_string=
        while [[ ${i:=0} < ${#original_string} ]]
        do
                random_char=${changed_string:$(($RANDOM%${#changed_string})):1}
                changed_string=${changed_string/$random_char/}
                echo ${#changed_string}
                recast_string=${recast_string}${random_char}
                ((i++))
        done
        echo "$1 has been recast to ${recast_string}."
}

recast $1
exit 0

However, I found a weird problem that if the length of string from the keyboard is less than 10, the script works well and if the length is equivalent to or greater than 10, the script works incorrectly that the loop is executed for only two times. Here is the debug results:
length is 9
Code:
sh -x recast_string j138sd113
+ recast j138sd113
+ local original_string=j138sd113
+ local changed_string=j138sd113
+ local recast_string=
+ [[ 0 < 9 ]]
+ random_char=8
+ changed_string=j13sd113
+ echo 8
8
+ recast_string=8
+ (( i++ ))
+ [[ 1 < 9 ]]
+ random_char=s
+ changed_string=j13d113
+ echo 7
7
+ recast_string=8s
+ (( i++ ))
...... (omitted sth here)
+ [[ 8 < 9 ]]
+ random_char=3
+ changed_string=
+ echo 0
0
+ recast_string=8sd311j13
+ (( i++ ))
+ [[ 9 < 9 ]]
+ echo 'j138sd113 has been recast to 8sd311j13.'
j138sd113 has been recast to 8sd311j13.

length is 10
Code:
 sh -x recast_string j138sd1135
+ recast j138sd1135
+ local original_string=j138sd1135
+ local changed_string=j138sd1135
+ local recast_string=
+ [[ 0 < 10 ]]
+ random_char=8
+ changed_string=j13sd1135
+ echo 9
9
+ recast_string=8
+ (( i++ ))
+ [[ 1 < 10 ]]
+ random_char=1
+ changed_string=j3sd1135
+ echo 8
8
+ recast_string=81
+ (( i++ ))
+ [[ 2 < 10 ]]
+ echo 'j138sd1135 has been recast to 81.'
j138sd1135 has been recast to 81.

I don't know why. Anybody can help me ? Appreciate that!SmilieSmilie
# 2  
Old 06-26-2013
Quote:
Originally Posted by franksunnn
Code:
+ [[ 2 < 10 ]]

In shell, < and its counterparts perform a string comparison. "2" is not less than "10" since "2" sorts after the character "1" in the string "10". For the desired numeric comparison, use -lt.

String comparisons: >, >=, <, <=, =, !=
Numeric comparisons: -gt, -ge, -lt, -le, -eq, -ne

Regards,
Alister
# 3  
Old 06-26-2013
Quote:
Originally Posted by alister
In shell, < and its counterparts perform a string comparison. "2" is not less than "10" since "2" sorts after the character "1" in the string "10". For the desired numeric comparison, use -lt.

String comparisons: >, >=, <, <=, =, !=
Numeric comparisons: -gt, -ge, -lt, -le, -eq, -ne

Regards,
Alister
I got you and it works now. And if I use ((2 < 10)) to check the condition of loop, that works well too. Thx!Smilie

---------- Post updated at 11:34 AM ---------- Previous update was at 10:43 AM ----------

Quote:
Originally Posted by alister
In shell, < and its counterparts perform a string comparison. "2" is not less than "10" since "2" sorts after the character "1" in the string "10". For the desired numeric comparison, use -lt.

String comparisons: >, >=, <, <=, =, !=
Numeric comparisons: -gt, -ge, -lt, -le, -eq, -ne

Regards,
Alister
Hi, I have another question.
If I want to pass the string containing whitespace like this "Hello World" to the script, what should I do?
Code:
./recast_string 1 3 5 
./recast_string "1 3 5"
./recast_string '1 3 5'

All above codes are incorrect and only the first part before the first whitespace can pass to the script.
Thx!
# 4  
Old 06-26-2013
Above codes 2 and 3 are correct. Call your function with double quoted $1, and it will fly!
# 5  
Old 06-26-2013
Quote:
Originally Posted by RudiC
Above codes 2 and 3 are correct. Call your function with double quoted $1, and it will fly!
Yes! I got it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Converting String To Integer/Float (weird case)

Hi guys, I'm new here. I have a problem at work. One of our scripts was eventually having a bug and only detected recently. Here's the issue and background: Bash Script which calls AWK script Awk script returns a string as per below (example):var1='00000-123'So, when we convert it, the... (18 Replies)
Discussion started by: sekfarok
18 Replies

2. Shell Programming and Scripting

Generating a Random String of 'n' length

Hi, How can I generate a string of random characters (alpha+numeric) of a particular length ? For e.g. for n=5, output = 'kasjf' n=10, output = 'hedbcd902k' Also, please let me know if random (valid) dates could also be generated. Thanks (7 Replies)
Discussion started by: rishigc
7 Replies

3. Shell Programming and Scripting

Weird awk problem

Hi, I have a simple awk script: BEGIN{} { $a=$2-$1; print $a } END{if(NR==0){ print "0" } } to which I provide the following input 2.9 14 22.2 27 (4 Replies)
Discussion started by: jamie_123
4 Replies

4. Shell Programming and Scripting

awk weird problem.

awk 'BEGIN{print 1.2.3.4}' 1.20.30.4 Can anyone explain why has extra "0" in the IP address? (3 Replies)
Discussion started by: newoz
3 Replies

5. Infrastructure Monitoring

Weird dependency problem!

Hi, I want to install net-snmp-devel package but i have following dependecy problem. It's very odd, i don't get it. One of packages is depended on the other one, the other one is depended on the previous one as well. :S :S Could you help me please? Here are the steps: # ls -l total... (4 Replies)
Discussion started by: oduth
4 Replies

6. Shell Programming and Scripting

Replace a random string of numbers

Hi Can someone help me with this one? I have string.. (PROC_PROC_ID == 12183) <--PID is dynamic and i want to replace the PID number with whatever PID from /opt/hpws/apache32_2/logs/httpd.pid file. i'm having problem since the PID on the string is dynamic. It may be 2-5 digits or more. ... (5 Replies)
Discussion started by: ryandegreat25
5 Replies

7. Shell Programming and Scripting

Replace a certain string with string containing random rubbish

Hello, in my shell script i have some multi-line text in a variable $TEMP - f.e. blablahblah blah blah bla TARGET hgloglo And i need to replace TARGET with text from another variable ($REPLACE), which is containing some text with nasty characters (\n, ", :, etc.) And stuff the altered text... (2 Replies)
Discussion started by: MilanCZ
2 Replies

8. UNIX for Advanced & Expert Users

Really weird delete problem

Hi, I've Ubuntu 8.04, and it has some files that I just cannot delete. I've tried everything, inode, fsck etc. Here is what the ls -li outputs root@ubuntu:/home/luser/.local/share/Trash/files/junk# ls -l ls: cannot access TRUNK_: No such file or directory ls: cannot access 2006_output.mv:... (11 Replies)
Discussion started by: nitin
11 Replies

9. Solaris

Weird crontab problem

Greetings To All! I am running Solaris 10 in a sparc environment. Here is the deal: In /var/spool/cron/crontabs, there is a cron user named "sys". If I do a crontab -l sys, it returns: # 0 * * * 0-6 /usr/lib/sa/sa1 # 20,40 8-17 * * 1-5 /usr/lib/sa/sa1 # 5 18 * * 1-5 /usr/lib/sa/sa2... (8 Replies)
Discussion started by: RobSand
8 Replies

10. UNIX for Dummies Questions & Answers

Weird Problem???

I have a problem I don't understand... I am trying to declare a variable, and then output the results of that variable, couldn't be simpler #!/bin/ksh VAR='Oranges' if then echo "Found Lemons" elif then echo "Found Oranges" fi The output shouold clearly be "Found Oranges", but... (2 Replies)
Discussion started by: danhodges99
2 Replies
Login or Register to Ask a Question