如何在 Bash 脚本中根据输入字符串删除子目录或创建文件?

写一个脚本,任意输入一个目录和一个字符串,对输入的字符串进行判断,如果字符串内容等于”ww”,则将该目录下的所有子目录删除,所有子文件拷贝到/var 目录下:如果字符串内容等于”rr”,则在该目录下创建 test1.txt 文件

#!/bin/bash
if [ x$1 == x ]
then
        read -p "请输入一个目录:" dir
else
                dir=$1
fi

read -p "请输入一个字符串:" str1
echo $str1","$dir

if [ -d $dir ]
then
        case $str1 in
        "ww")
                for file in `ls $dir`
                do
                        file_path=$dir/$file
                        if test -f $file_path
                        then
                                cp $file_path /var
                        elif test -d $file_path
                        then
                                rm -rf $file_path
                        fi
                done
                ;;
        "rr")
                cd $dir &&`touch test1.txt`
                ;;
        *)
                echo "未知的输入,请输入rr或ww..."
                ;;
        esac
else
        echo "请输入一个目录..."
fi
阅读 973
avatarAI BotBETA

以下是按照您的要求编写的shell脚本,它接收一个目录和一个字符串作为输入,并根据字符串的内容执行相应的操作:

#!/bin/bash

# 检查是否提供了目录作为参数
if [ -z "$1" ]; then
    read -p "请输入一个目录: " dir
else
    dir="$1"
fi

# 读取用户输入的字符串
read -p "请输入一个字符串: " str1

# 打印出输入的字符串和目录
echo "$str1,$dir"

# 检查目录是否存在
if [ -d "$dir" ]; then
    case "$str1" in
        "ww")
            # 遍历目录下的所有文件和子目录
            for file in "$dir"/*; do
                file_path="$file"
                if [ -f "$file_path" ]; then
                    # 如果是文件,则拷贝到/var目录
                    cp "$file_path" /var/
                elif [ -d "$file_path" ]; then
                    # 如果是子目录,则删除它
                    rm -rf "$file_path"
                fi
            done
            ;;
        "rr")
            # 如果是"rr",则在目录中创建test1.txt文件
            touch "$dir/test1.txt"
            ;;
        *)
            echo "未知的输入,请输入rr或ww..."
            ;;
    esac
else
    echo "请输入一个存在的目录..."
fi

请注意,这个脚本有几个潜在的问题和改进点:

  1. 权限问题:直接拷贝文件到/var目录可能需要root权限,如果脚本不是以root用户身份运行,可能会失败。
  2. 错误处理:在删除目录或创建文件时,没有错误处理。例如,如果/var目录已满,则cp命令会失败。
  3. 文件覆盖:如果/var目录下已经存在与源目录中同名的文件,则它们会被覆盖。
  4. 命令执行顺序:在rr的情况下,cd $dir && touch test1.txt可以简化为touch "$dir/test1.txt",因为touch命令可以直接指定文件路径。
  5. 安全性:此脚本没有进行输入验证,可能存在安全风险。在生产环境中使用时,需要更加谨慎地处理用户输入。
  6. 性能:使用lsfor循环可能不是遍历目录的最有效方式,特别是当目录包含大量文件时。使用find命令或其他更高效的工具可能更为合适。

在实际使用中,需要根据具体的场景和需求来调整和完善这个脚本。

1 个回答

看是否符合。

#!/bin/bash

# 检查是否提供了目录作为参数
if [ -z "$1" ]; then
    read -p "请输入一个目录: " dir
else
    dir="$1"
fi

# 读取用户输入的字符串
read -p "请输入一个字符串: " str1

# 打印出输入的字符串和目录
echo "$str1,$dir"

# 检查目录是否存在
if [ -d "$dir" ]; then
    case "$str1" in
        "ww")
            # 遍历目录下的所有文件和子目录
            for file in "$dir"/*; do
                file_path="$file"
                if [ -f "$file_path" ]; then
                    # 如果是文件,则拷贝到/var目录
                    cp "$file_path" /var/
                elif [ -d "$file_path" ]; then
                    # 如果是子目录,则删除它
                    rm -rf "$file_path"
                fi
            done
            ;;
        "rr")
            # 如果是"rr",则在目录中创建test1.txt文件
            touch "$dir/test1.txt"
            ;;
        *)
            echo "未知的输入,请输入rr或ww..."
            ;;
    esac
else
    echo "请输入一个存在的目录..."
fi
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进