-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_folders.sh
58 lines (50 loc) · 1.41 KB
/
generate_folders.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#! /bin/bash
#---#---#---#---#---#---#---#---#---#---#
# Code courtesy of jilles via StackOverflow:
# http://stackoverflow.com/a/3951175
case $1 in
''|*[!0-9]*) echo "Argument 1 must be an integer value." >&2 ; exit 2 ;;
*) ;;
esac
#---#---#---#---#---#---#---#---#---#---#
# Note: Only checks that a filename is valid for ext4 filesystems.
# Not guaranteed to ensure filenames are valid on other filesystems.
# Admittedly, it might not even ensure valid filenames on ext4.
for i in ${@:2}; do
case $i in
''|*[/]*) echo "'$i' is not a valid filename." >&2 ; exit 2 ;;
*[![:print:]]*) echo "'$i' is not a valid filename." >&2 ; exit 2 ;;
*) ;;
esac
done
gen_files=${@:2}
format_length=${#1}
echo "This script will create ${1} folders, numbered from `printf "%0${format_length}d" 1` to ${1}."
if [ $# -ge 2 ]; then
echo "Additionally, the following empty files will be created in each folder:"
file_list=" "
for i in $gen_files; do
file_list+=" $i"
if [ ${#file_list} -ge 80 ]; then
echo $file_list
file_list=" "
fi
done
if [ ${#file_list} -gt 2 ]; then
echo $file_list
fi
fi
read -p "Is the above information correct? [y/n]" ans
case $ans in
[Yy]* ) ;;
*) echo "Program terminated by user." >&2 ; exit 1 ;;
esac
range={`printf "%0${format_length}d" 1`..$1}
for d in `eval echo $range`; do
mkdir $d
cd $d
for f in $gen_files; do
touch $f
done
cd ..
done