Reputation: 33
I need to create a number of folders with the same name but with a number at the end.
User should write a number and the script should create these numbers of folders. I don't know how to link the number and the number os folders.
Here is my script
#!/bin/bash
echo "(1617S2)" A.C.B.S
pwd
date
NOM="A.C.B.S"
echo $NOM
echo -n "Introduce el numero de carpetas que quieras :"
read x
if (($x<=10)); then
echo "Son $x carpetas"
else (($ <10))
echo -n "El número de carpetas tiene que ser entre 1 i 10 :"
read x2
echo "Son $x2 carpetas"
fi
cd ..
cd /var
sudo mkdir carpetaprincipal
cd ..
cd /var/carpetaprincipal
sudo mkdir carpeta {1..10}
Upvotes: 3
Views: 127
Reputation: 49784
To do multiple mkdir
while looping a variable number of times:
x2=4
i=1
while [ "$i" -le "$x2" ]
do
sudo mkdir carpeta$1
i=$(($i + 1))
done
Upvotes: 0
Reputation: 47169
You could use seq
with xargs
to iterate and create the number of folders chosen for the input:
#!/bin/bash
echo "(1617S2)" A.C.B.S
pwd ; date
NOM="A.C.B.S" ; echo $NOM
function makeFolders () {
echo -n "Introduce el numero de carpetas que quieras :"
read -r x
if [[ "$x" -lt 11 ]] && [[ "$x" -gt 0 ]]; then
echo "Son $x carpetas"
cd ../var || exit
mkdir carpetaprincipal
cd carpetaprincipal || exit
seq "$x" | xargs -I{} mkdir carpeta_{}
fi
if [[ "$x" -lt 1 ]] || [[ "$x" -gt 10 ]]; then
echo "El número de carpetas tiene que ser entre 1 i 10!"
makeFolders # you could optionally exit 1 here
fi
}
makeFolders
There were some other issues with your script, mainly the logic didn't make sense. If you put in more than 10 or less than 1 then the script still allowed the user to create folders which are above are below what is supposed to be allowed. Putting those methods within in a function also allows you to return to the input line if there is an error. Also, read
should include -r
, otherwise it has the potential to mangle backslashes.
Upvotes: 2