Borja León Borja León - 8 days ago 8
Bash Question

Create a random name for the complete script

I have a script that joins several

csv
files into an output file called
merged_t*.csv


Here is the script:

for i in $(ls -latr sample_*.csv);
do
paste -d, $i >> out_$RANDOM.csv;
done

sed 's/^|$/\x27/g' out_$RANDOM.csv | paste -d, > merged_t$RANDOM.csv


The
"$RANDOM"
in the first command
"out_$RANDOM"
must be the same that the
"out_$RANDOM"
in the second.

How can i do it?

Answer

declare your own variable first:

myrandom=$RANDOM
for i in $(ls -latr sample_*.csv); do
    paste -d, $i >> out_${myrandom}.csv;
done
sed 's/^|$/\x27/g' out_${myrandom}.csv | paste -d, > merged_t${myrandom}.csv