如何从文件中获取子目录列表,然后在目录中创建这些子目录?

Srk93

当用户输入名称时,应该在该名称下创建一个新目录。
除此之外,该脚本需要咨询文件,structure1.txt这是在发现/etc/scriptbuilder/str1
在此文件中,它将列出两个子目录(每行一个),然后该脚本应在用户刚刚创建并命名的新目录中创建这两个子目录。

那么脚本如何创建该文本文件中列出的每个子目录?
我完全迷失了。

到目前为止,这是我的代码:

 echo "Enter the project name "
 read name
 echo $name

 if [ ! -d $name ] then
 mkdir $name

 else 
 echo "The project name you entered already exists"
 fi

 cp /etc/scriptbuilder/str1/structure1.txt /$name 
 #I know this is wrong 
 because this would just copy the file over to the new directory but not actually 
 make the two subdirectories that are on the file onto the new directory
法案

您正在寻找的bash命令是read
另外,您的语法if [ ! -d "$name" ]应使用分号。
else将通常具有一个exit 1(或一些这样的值)。
典型的bash代码从命令行获取输入,但是您想要的就可以了。

为了进行测试,我插入了一个~(代字号),它引用了您的主目录。

该脚本应类似于:

filename="/etc/scriptbuilder/str1"
read -p "Enter the project name " name
echo "$name"
if [ ! -d ~/"$name" ]; then
  mkdir ~/"$name"
else 
  echo "The project name you entered already exists"
  exit 1
fi
while read -r line; do
  mkdir ~/"$name/$line"
done < "$filename"

您可以清理格式。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章