Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
hi,
the below is my code
in this code my A2 file have some values like test1,test2,test3 etc and in the temp variable iam randomly shuffling the order of A2 file elements each time when running the script
like one time when run the script it shuffle the order randomly
test2,test3,test1
second time when script run again it shuffle the order randomly
test3,test1,test2
like that each time when script is running it shuffle the order randomly
now i want to pass the temp variable which have A2 file value to the another file A3 iam able to pass the value but it is always showing the same order when every time iam running the script but i want that each time when script will run value of file A3 should be changed randomly when A2 file value should be shuffled randomly because indirectly iam passing the A2 file value to the A3 file
please do the neeful
#!/bin/sh
APP_INSTANCE_ID=56
file1=/Users/hussain/Desktop/shells/A2 //here A2 is the text file which have value test1,test2,test3
file2=/Users/hussain/Desktop/shells/A3 ////A3 is the text file in which A2 file value is coming
id=`cat $file1 | cut -d "=" -f 2`
sed "s/$temp//g" $temp > $l_tmp_web_config_file2 //in this line value of A2 is coming in the A3 file but it is
always showing the same order each time when script is running
hi,
this is my file1 which have below contents
test=test1,test2,test3,test4,test5
hi
hello
world
now i want to display the contents of file1 into another file2 here file1 contents of first line is shuffling randomly i want the output like that each time when script will run
first time when script run
test=test2,test1,test3,test4,test5//here value is randomly shuffling
hi
hello
world
second time when script will run
test=test4,test3,test2,test1,test5//here value is again randomly shuffling
hi
hello
world
but when iam running the script output is comming like that
test=test4,test3,test2,test1,test5,hi,hello,world
is there any solution
hussain.s, please use [code][/code] around your code, to preserve formatting and to improve readability.
Here's a bash-builtin only way to shuffle array elements. I wouldn't try it on very large arrays though, as it uses a simple brute-force testing loop to keep it from creating duplicates. I used the comma-separated input list given above.
Code:
IFS=","
test="test1,test2,test3,test4,test5"
array1=( $test )
num=${#array1[@]}
complete=""
for i in ${!array1[@]}; do
while true; do
newi=$(( RANDOM%num ))
if [[ ! $complete =~ "$newi:" ]]; then
array2[$newi]="${array1[$i]}"
complete="${complete}${newi}:"
break
fi
done
done
echo "${array2[*]}"
# [#] here will the entire array as a single string, using the current IFS as separator. Commas in this case. Must be quoted.
# [@] will print each element individually separated by spaces.
There are probably better ways to do it.
Last edited by David the H.; 10-11-2010 at 11:07 AM.
Reason: fixed minor error
I just discovered that there's a gnu coreutils application called shuf that can be used to randomize an input sequence. Here's a greatly simplified version of my last script that uses it.
Code:
teststring="test1,test2,test3,test4,test5,test6"
array1=( ${teststring//,/ } )
num=$(( ${#array1[@]} -1 ))
newseq=$( shuf -i 0-$num )
for i in $newseq; do
outstring+="${array1[$i]},"
done
echo ${outstring%,}
Much cleaner overall, although it does now depend on an external tool. But at least that tool is a core utility. You'll notice I've also eliminated messing around with the IFS.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.