shell之創(chuàng)建文件及內容的方法示例
更新時間:2020年05月08日 10:27:57 作者:謙遜的鉛筆
這篇文章主要介紹了shell之創(chuàng)建文件及內容的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
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 "文件夾已經存在" 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 "文件夾已經存在" fi
調用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 "當前只能創(chuàng)建controller和model文件" fi echo "" >> $dirAndName echo "class $fileName{" >> $dirAndName echo " //" >> $dirAndName echo "}" >> $dirAndName echo "?>" >> $dirAndName echo "文件創(chuàng)建完成" else echo "文件已經存在" 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 "文件已經存在" fi fi
調用shell創(chuàng)建文件:
[root@vbox-nginx shell_command]# ./mkfile.sh ApiLoveHouse/Controllers WelcomeController //上級文件夾 要創(chuàng)建的文件名 文件創(chuàng)建完成
shell 在已有文件中追加多行內容
通過 cat>>文件<<EOF EOF 來實現文件追加多行內容
執(zhí)行
cat >>/test/appendLine.conf<<EOF 我是第二行 我是第三行 EOF
顯示結果為:
到此這篇關于shell之創(chuàng)建文件及內容的方法示例的文章就介紹到這了,更多相關shell 創(chuàng)建文件及內容內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
linux下自動備份MySQL數據并上傳到FTP上的shell腳本
linux下自動備份MySQL數據并上傳到FTP上的shell腳本,需要的朋友可以參考下2013-01-01零基礎入門篇之Linux及Arm-Linux程序開發(fā)筆記
這篇文章主要介紹了零基礎入門篇之Linux及Arm-Linux程序開發(fā)筆記,需要的朋友可以參考下2015-10-10