#!/bin/bash N="$1" D="$2" K="$3" ID="$4" if [ -z "$N" ] || [ -z "$D" ] || [ -z "$K" ] || [ -z "$ID" ]; then echo "Usage of : $0 <N> <D> <K> <ID> Please provide proper arguments." exit 1 fi folder_name="cs527_lab1_test_$ID" normal_file_count=0 folder_count=0 max_depth=$D max_levels=0 function create_files_and_subfolders { local current_folder="$1" local depth="$2" if [ "$depth" -gt "$max_levels" ]; then max_levels="$depth" fi L=$N for ((i = 1; i <= L; i++)); do file_type=$((RANDOM % 2)) if [ $file_type -eq 0 ] && [ "$depth" -le $max_depth ]; then folder_count=$((folder_count + 1)) mkdir "$current_folder/dir$i" ((depth++)) create_files_and_subfolders "$current_folder/dir$i" "$((depth))" else normal_file_count=$((normal_file_count + 1)) file_path="$current_folder/file$i.txt" touch "$file_path" max_chars=$((1024 * K)) text_length=$((RANDOM % (max_chars + 1))) random_text=$(head /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c "$text_length") echo "$random_text" > "$file_path" fi done } if [ -d "$folder_name" ]; then rm -rf "$folder_name" fi mkdir "$folder_name" create_files_and_subfolders "$folder_name" 1 readme_file="$folder_name/readme.txt" echo "Total normal files $((normal_file_count + 1))" >> "$readme_file" echo "Total folders $folder_count" >> "$readme_file" echo "Maximum levels $max_levels" >> "$readme_file"
Write, Run & Share Bash code online using OneCompiler's Online Bash Shell for free. It's one of the robust, feature-rich Bash shell available over online and getting started with the OneCompiler's Bash Shell is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Bash
. OneCompiler also has reference scripts, where you can look for the sample scripts and start coding.
Bash (Bourne Again Shell) is a shell program written by Brian Fox and is an upgraded version of Bourne Shell program 'sh'.
name="Foo"
echo $name
echo "$name"
echo "${name}"
if [ conditional-expression ]
then
statements
fi
if [ conditional-expression ]
then
statements
else
statements
fi
if [ conditional-expression ]
then
statements
elif [ conditional-expression ]
then
statements
else
statements
fi