Sponsored Content
Top Forums Shell Programming and Scripting A very weird problem about getting a random string Post 302826321 by franksunnn on Wednesday 26th of June 2013 10:23:02 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
TPING(1)							   LAM COMMANDS 							  TPING(1)

NAME
tping - Send echo messages to LAM nodes. SYNOPSIS
tping [-hv] [-c count] [-d delay] [-l length] nodes OPTIONS
-h Print the command help menu. -v Turn OFF verbose mode. -c count Send count messages. -d delay Delay delay seconds between each message. -l length Each message is length bytes long. DESCRIPTION
The tping command sends messages to, and collects replies from, a list of nodes, via the LAM echo server. It is similar to the UNIX ping(8) command, and is used as a quick diagnosis of the LAM network. Unless options are specified, tping sends a 1 byte message an infinite number of times, displaying the roundtrip time of each message as it completes, with a delay of 1 second between roundtrips. After the loop is broken (with keyboard interrupt, eg: ^C), tping prints statis- tics about all roundtrip messages. EXAMPLES
tping h Echo messages to the local node. tping -v n7 -l 1000 -c 10 Echo 1000 byte messages to node 7. Stay silent while working. Stop after 10 roundtrips and report statistics. BUGS
There is no built-in timeout and tping will wait forever to receive an echo. If no echo is received, due to a dead link or node, tping hangs. Stop the process with a keyboard suspend signal (eg: ^Z) and terminate LAM with lamhalt(1) or lamwipe(1) (although the use of lamwipe(1) is deprecated). SEE ALSO
lamhalt(1), lamwipe(1) LAM 7.1.4 July, 2007 TPING(1)
All times are GMT -4. The time now is 04:03 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy