Home > Back-end >  Repeat through bash script based on user input
Repeat through bash script based on user input

Time:11-12


#sets dir name 
echo "What is the name of the target?"
read targetName

#changes dir to desktop
mkdir -p ../Desktop/Notes
cd ../Desktop/Notes

#make working directory
mkdir $targetName
cd $targetName
mkdir "IPs" "SubDomains" "Screenshots" "NmapScans" "Notes"

I have been trying to wrap my brain around simple loops in bash. I have the following script I would like to ask a user for "targetName" to create some directories. After the directories are created I would like the script to ask the user if they would like to create another target, If Y/Yes loop back, if no then exit. I realize this is a fairly simple issue, new to bash and programming in general and I work best if I create the problem myself. Im 99% sure I need a if loop for this. Im just not sure how to break it up correctly. Thanks in advance!

CodePudding user response:

See if that helps you:

while true
do
    printf 'What is the name of the target? '
    IFS= read -r targetName

    # sanity checks:
    # - no empty string
    # - no '/'
    [ ${#targetName} -eq 0 ] || [[ ${targetName} == */* ]] && continue

    # for now, just print what you'll be doing
    printf 'mkdir -p \\\n'
    printf '  %q \\\n' ~/Desktop/Notes/"$targetName"/{IPs,SubDomains,Screenshots,NmapScans,Notes}

    printf 'Do you wish to create an other target?[y/n] '
    read -n 1 yesno
    echo

    case "$yesno" in
        [Yy]) continue;;
        *) break;;
    esac
done
  • Related