text 测试shell脚本以创建文件夹/文件,基于Brad Traversy教程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 测试shell脚本以创建文件夹/文件,基于Brad Traversy教程相关的知识,希望对你有一定的参考价值。

# If the folder does not exist, create it, then create the file in that folder
# and add the text.

# If the folder exists and the file dosen't then create the file in that folder 
# and add the text.

# If the folder and file exist, then just add the text to the file.   

FOLDER="hello4"
FILE="world2.txt"
TEXT_ADDED="Hello World4" 

if [ -e "$FOLDER" ]
then
  echo "Folder $FOLDER exists"
  cd $FOLDER

  if [ -e "$FILE" ]
  then
    echo "File $FILE exists"
    echo "$TEXT_ADDED" >> "$FILE"
    echo "Added the text '$TEXT_ADDED' to $FILE"
    echo "Files currently in folder $FOLDER, $(ls)"
  else
    touch $FILE
    echo "$TEXT_ADDED" >> "$FILE"
    echo "Changed directory to $FOLDER, Created $FILE and added the text '$TEXT_ADDED'"
    echo "Files currently in folder $FOLDER, $(ls)"
  fi  

else
  mkdir $FOLDER
  echo "$TEXT_ADDED" >> "$FOLDER/$FILE"
  echo "Created folder and file $FOLDER/$FILE and added the text '$TEXT_ADDED'"
  echo "Files currently in folder, $FOLDER $(ls)"
fi

以上是关于text 测试shell脚本以创建文件夹/文件,基于Brad Traversy教程的主要内容,如果未能解决你的问题,请参考以下文章

使用 shell 脚本迭代 json 以存储键值对

编写Shell脚本---接受、判断用户参数

shell 测试URL 是否正常脚本

求Bash Shell脚本,判定文件是不是存在。

Shell脚本-if语句

shell--构建基本脚本