Linux简单Shell脚本编程

echo"**cmdmenu**"echo"1----日期"echo"2----ls"echo"3----谁"echo"4----pwd"echo"选择::"readnumberif[1==$number];thendate;if[2==#number];thenls;if[3==$number];thenwho;if[4==#number];thenpwd;elseecho"youarewrong"fi我没有调试过这个程序,我只是给你一个想法,你可以按照我的框架,自己调试一下,希望对你有帮助,谢谢!

如何编写简单的Shell脚本文件之Linux的基本操作

要编写shell脚本,首先要具备Linux命令的基础知识,如何插入文件、如何运行文件、存在哪些命令等。

我们有很多shell类型,常见的shell环境包括sh、bash、csh、zsh等。
Linux中最常见的脚本是sh或shell。
在shell脚本中,必须在一开始就指定shell环境。
这样我们就有了shell的Shabang:

/bin/sh或/bin/bash

shell脚本的格式:Shell脚本通常命名为*.sh上面的权限有可执行权限x,即执行命令chmodu+x*.sh


行:3种类型:

shscript-path/script-name

cd-script-path&&./script-name

surscript-path/script

写一个最简单的脚本:

[root@linuxprobe~]#vim1.sh

/bin/sh

echo"thisismyfristscripts,越来越多linux,您可以阅读“这就是您应该学习的方式Linux""

[root@linuxprobe~]#chmodu+x1.sh

[root@linuxprobe~]#./1.sh

thisismyfristscripts,moreandmorelinux,你可以阅读《你应该如何学习Linux》

学习Linux需要多学习多实践

shell脚本执行linux命令

Linux下测试脚本中执行命令的方式主要有四种。
具体如下:方法一:切换到shell脚本所在目录(也是操作目录)执行shell脚本:cd/data/shell./hellosh方法二:可以使用绝对方法执行bashshell脚本,具体代码为:/data/shell/hello.sh方法三:可以选择直接使用。
bash命令或sh命令执行bashshell脚本,有具体代码:cd/data/shellbashhello.sh或cd/data/shellshhello.sh方法四:也可以选择执行在shell环境下运行的bashshell脚本。
sh说明:执行shell脚本的前三种方法都需要在当前shell(即父shell)打开的子shell环境中执行。
直接在当前shell中执行该方法。