当前位置: 移动技术网 > 网络运营>服务器>Linux > shell之创建文件及内容的方法示例

shell之创建文件及内容的方法示例

2020年06月23日  | 移动技术网网络运营  | 我要评论

shell之创建文件夹:

[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 "创建文件夹成功"
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 "创建文件夹成功"
else
echo "文件夹已经存在"
fi

调用shell创建文件夹:

[root@vbox-nginx shell_command]# ./mkdir.sh apilovehouse model    //上级文件夹 要创建的文件夹名
创建文件夹成功

shell之创建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 "当前只能创建controller和model文件"
fi

echo "" >> $dirandname
echo "class $filename{" >> $dirandname
echo "     //" >> $dirandname
echo "}" >> $dirandname
echo "?>" >> $dirandname
echo "文件创建完成"
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 "文件创建完成"
else
echo "文件已经存在"
fi

fi

调用shell创建文件:

[root@vbox-nginx shell_command]# ./mkfile.sh apilovehouse/controllers welcomecontroller    //上级文件夹 要创建的文件名
文件创建完成

shell 在已有文件中追加多行内容

通过 cat>>文件<<eof eof 来实现文件追加多行内容


执行

cat >>/test/appendline.conf<<eof
我是第二行
我是第三行
eof

显示结果为:

到此这篇关于shell之创建文件及内容的方法示例的文章就介绍到这了,更多相关shell 创建文件及内容内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网