【问题标题】:Repeat through bash script based on user input根据用户输入重复 bash 脚本
【发布时间】:2021-12-24 09:32:04
【问题描述】:

#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"

我一直试图将我的大脑包裹在 bash 中的简单循环上。我有以下脚本我想向用户询问“targetName”以创建一些目录。创建目录后,我希望脚本询问用户是否要创建另一个目标,如果是/是则循环返回,如果否则退出。我意识到这是一个相当简单的问题,对 bash 和一般编程来说都是新的,如果我自己创建问题,我会工作得最好。我 99% 确定我需要一个 if 循环。我只是不确定如何正确分解它。提前致谢!

【问题讨论】:

    标签: bash loops shell if-statement


    【解决方案1】:

    看看对你有没有帮助:

    #!/bin/bash
    
    while true
    do
        IFS= read -p 'What is the name of the target? ' -r targetName
    
        # sanity checks:
        # * no empty input
        # * no '/' in input
        if [ ${#targetName} -eq 0 ] || [[ ${targetName} == */* ]]
        then
            echo 'error: invalid target name' 1>&2
            continue
        fi
    
        # for now, just print what you'll be doing
        printf 'mkdir -p \\\n'
        printf '  %q \\\n' ~/Desktop/Notes/"$targetName"/{IPs,SubDomains,Screenshots,NmapScans,Notes}
    
        read -p 'Do you wish to create an other target?[y/n] ' -n 1 yesno
        echo
    
        case "$yesno" in
            [Yy]) continue;;
            *) break;;
        esac
    done
    

    【讨论】:

    • 谢谢你!我看到一些我不太熟悉的命令似乎可以解决问题。
    猜你喜欢
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多