Hi,
I have a file nexus-1234 in a directory. I want to generate a random number and replace the 1234 with it and rename the file.
So nexus-1234 becomes nexus-2863 after running the script.
Any help is appreciated. Thanks in advance.
Hi,
I have a file nexus-1234 in a directory. I want to generate a random number and replace the 1234 with it and rename the file.
So nexus-1234 becomes nexus-2863 after running the script.
Any help is appreciated. Thanks in advance.
bash:
var="nexus-1234"
new_var= ${var/[0-9]*//}$( printf "%04d" ${RANDOM})
mv $var $newvar
Note: RANDOM provides random numbers in the range of 1 - 32767, so some numbers will be larger than 9999.
Hi Jim,
It's giving an error saying...
Command '"./changeBatchID.sh"'
failed with return code 0 and error message
./changeBatchID.sh: line 15: nexus-/27041: No such file or directory
mv: missing destination file operand after `nexus-1234'
Try `mv --help' for more information..
Here is the code I have so far...
for filename in ./*
do
if [ -f "$filename" ]
then
file="${filename#*/}";
#echo $file >> $LOGFILE
if [[ "$file" == nexus* ]]
then
var="nexus-1234"
new_var= "${var/[0-9]*//}$( printf "%04d" ${RANDOM})";
mv $var $newvar
fi
fi
done;