shell之創(chuàng)建文件及內(nèi)容的方法示例
shell之創(chuàng)建文件夾:
[root@vbox-nginx shell_command]# vi ./mkdir.sh #!/bin/sh parentDir="/media/sf_Project/self/smarty-frame/application/$1" fileName=$2 dirAndName=$parentDir/$fileName if [ ! -d "$dirAndName" ];then mkdir $dirAndName echo "創(chuàng)建文件夾成功" else echo "文件夾已經(jīng)存在" fi [root@vbox-nginx shell_command]# cat ./mkdir.sh #!/bin/sh parentDir="/media/sf_Project/self/smarty-frame/application/$1" fileName=$2 dirAndName=$parentDir/$fileName if [ ! -d "$dirAndName" ];then mkdir $dirAndName echo "創(chuàng)建文件夾成功" else echo "文件夾已經(jīng)存在" fi
調(diào)用shell創(chuàng)建文件夾:
[root@vbox-nginx shell_command]# ./mkdir.sh ApiLoveHouse Model //上級文件夾 要創(chuàng)建的文件夾名 創(chuàng)建文件夾成功
shell之創(chuàng)建php文件:
[root@vbox-nginx shell_command]# vi ./mkfile.sh #!/bin/sh parentDir="/media/sf_Project/self/smarty-frame/application/$1" fileName=$2 dirAndName="$parentDir/$fileName.php" string=${parentDir#*application} namespace=$(echo $string | sed 's#\/#\\#g') echo $string echo $namespace if [ ! -d "$parentDir" ];then echo "父級文件夾路徑錯誤" else cd $parentDir if [ ! -f "$dirAndName" ];then touch $dirAndName echo "<?php" > $dirAndName if [[ $fileName == *$strCon* ]];then touch $dirAndName echo "<?php" > $dirAndName if [[ $fileName == *$strCon* ]];then echo "namespace App$namespace;" >> $dirAndName elif [[ $fileName == *$strMod* ]];then echo "namespace App\$namespace;" >> $dirAndName else echo "當(dāng)前只能創(chuàng)建controller和model文件" fi echo "" >> $dirAndName echo "class $fileName{" >> $dirAndName echo " //" >> $dirAndName echo "}" >> $dirAndName echo "?>" >> $dirAndName echo "文件創(chuàng)建完成" else echo "文件已經(jīng)存在" fi fi fi
或
#!/bin/sh parentDir=$1 fileName=$2 dirAndName="$parentDir/$fileName.php" if [ ! -d "$parentDir" ];then echo "父級文件夾路徑錯誤" else cd $parentDir if [ ! -f "$dirAndName" ];then cat>$dirAndName<<EOF <?php namespace App; class $fileName{ // } ?> EOF echo "文件創(chuàng)建完成" else echo "文件已經(jīng)存在" fi fi
調(diào)用shell創(chuàng)建文件:
[root@vbox-nginx shell_command]# ./mkfile.sh ApiLoveHouse/Controllers WelcomeController //上級文件夾 要創(chuàng)建的文件名 文件創(chuàng)建完成
shell 在已有文件中追加多行內(nèi)容
通過 cat>>文件<<EOF EOF 來實(shí)現(xiàn)文件追加多行內(nèi)容
執(zhí)行
cat >>/test/appendLine.conf<<EOF 我是第二行 我是第三行 EOF
顯示結(jié)果為:
到此這篇關(guān)于shell之創(chuàng)建文件及內(nèi)容的方法示例的文章就介紹到這了,更多相關(guān)shell 創(chuàng)建文件及內(nèi)容內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Linux traceroute命令使用方法實(shí)例詳解
這篇文章主要介紹了Linux traceroute命令使用方法實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解應(yīng)用這部分內(nèi)容, 需要的朋友可以參考下2017-10-10linux下自動備份MySQL數(shù)據(jù)并上傳到FTP上的shell腳本
linux下自動備份MySQL數(shù)據(jù)并上傳到FTP上的shell腳本,需要的朋友可以參考下2013-01-01零基礎(chǔ)入門篇之Linux及Arm-Linux程序開發(fā)筆記
這篇文章主要介紹了零基礎(chǔ)入門篇之Linux及Arm-Linux程序開發(fā)筆記,需要的朋友可以參考下2015-10-10