我的代码应该要求用户输入他们想要创建的目录的名称,然后应该要求编辑目录中的文件,但是当我创建目录后脚本不会继续,我看不到任何错误,但用新的眼光来批评代码总是更容易。
我还将文件添加到目录中,但它从不询问我是否要编辑它们。
#!/bin/bash
#Testing to see if input is empty
if [ $# -lt 1 ]; then
echo "Empty Directory will be created"
fi
#Get the name of the directory by the user, also creating a variable named directory
read -p "Please enter the name of the drectory you wish to create: " directory
#Check if the directory exists, if it doesn't it will be created in the Home folder
if [ ! -d ~/$directory ]; then
#Creating the directory if it doesnt exist
mkdir ~/$directory/
fi
#Create files individually in the directory
for i in "$@"; do
touch ~/$directory/$i
#Asking the user if they wish to edit the files they have created inside the directory
read -p "edit file $i (Y/N)? " edit
#If they answer yes then read the lines entered by the user
if [["$edit" = "Y" || "$edit" = "y"]]; then
line=""
#Stores the amount of words added to the file
count=0
#Reads the lines enetered by the user
echo "Please enter your text to be added into the file (Enter \"end\" to exit the editing):"
read line
#The script will keep reading the words entered in the file until the user initiates the end command "end"
while ["$line" != "end"]; do
#repeat the words entered into the file
echo "$line" >> ~/directory/$i
#Get the amount of words entered into the file
count=$(($count + $(wc -w <<< $line)))
#read the next line from user input
read line
done
echo "$count words have been written to the file"
fi
done
答案1
改变这一行
if [["$edit" = "Y" || "$edit" = "y"]]; then
有了这一行:
if [[ "$edit" = "Y" || "$edit" = "y" ]]; then
[[ ]] 之后缺少一个空格。
另外:最好使用 $HOME 而不是 ~