Zamíchání řádek v souboru
Zajímavý script, pomocí kterého zamícháte náhodně řádky v souboru:
#!/bin/bash
# function to display usage message and exit
usage()
{
cat < < eof
$(basename $0) input_filename.txt
eof
exit 1
}
# if number args less than 1...
if [ $# -lt 1 ]; then
usage
fi
# if first arg isn't a file
if [ ! -f $1 ]; then
usage
fi
# open file
exec 3<$1
# declare an array and an integer
declare -a linearray
declare -i counter=0
# read file to linearray
while read line <&3; do
linearray[${counter}]=$line
((counter++))
done
#close file
exec 3>&-
for (( i=0; i < $counter; i++ )); do
switch=1
#get number not already used
while [ $switch -eq 1 ]; do
switch=0
number=$(($RANDOM % $counter))
for num in $linenolist; do
if [ $num -eq $number ]; then
switch=1
fi
done
done
linenolist="$linenolist $number"
echo ${linearray[$number]} >> testoutput.txt
done
echo "DONE"