I am writing the following shell script for generating multiple directories by providing arguments. This is my shell script "createDirectories1.sh"
#!/bin/bash
echo "$1"
echo "$2"
echo "$3"
mkdir $1{$2..$3}
#command mkdir $1{$2..$3}
And I am running the above script using following command
bash createDirectories1.sh week 1 5
And my expected output is Expected output
This is how my terminal looks when giving commands to execute the script
I am not sure when I am running this command mkdir week{1..5} it works fine but when i run the same using shell script its not working
please help and let me know what modification is needed in my shell script ??
CodePudding user response:
You must use eval
command. The eval command first evaluates the argument and then runs the command stored in the argument.
#!/bin/bash
echo "$1"
echo "$2"
echo "$3"
eval mkdir $1{$2..$3}
CodePudding user response:
this is what you need.
mkdir week_{1..5}
Let you know this answer : https://askubuntu.com/questions/731721/is-there-a-way-to-create-multiple-directories-at-once-with-mkdir
CodePudding user response:
bash ranges work with literals, not variables, so you need some way to eval
{$2..$3}
; the problem with eval
ing a string is that you have to ensure that it won't bite you:
#!/bin/bash
declare -a dirs="( $(printf '%q{%q..%q}' "$1" "$2" "$3") )"
mkdir "${dirs[@]}"
CodePudding user response:
Use for loop:
for ((i=$2; i<=$3; i )); { mkdir "$1$i"; }