如何在linux上编译和执行shell脚本?

创建脚本:vim test.sh,第一行写入!/bin/bash。

授予权限:chmod +x test.sh。

执行脚本:./test.sh 或 sh test.sh。

别忘了添加执行权限,否则就是陷阱。

linux执行sh脚本的指令有哪些?

Yesterday, I configured a scheduled task on the server, and suddenly I wanted to try a different execution method. I tried the first method first, typing cd /data/shell in the /data/shell directory, and then entering ./hello.sh.当然,剧本执行得很顺利。
奇怪的是,我在PATH环境变量中没有找到/data/shell,但是添加./前缀后,bash却找到了该脚本,这让我有点困惑。
Then I tried the second method and entered /data/shell/hello.sh directly, and there was no problem this time. I'm kind of curious, do paths in PATH really matter?然后我尝试了第三种方法,先cd到/data/shell目录,然后输入bash hello.sh和sh hello.sh。
这两个命令还可以执行脚本,而且我发现此时不需要设置脚本执行权限,也不需要在脚本中指定bash路径。
It suddenly occurred to me that this is because I'm running in a bash environment, and bash will parse these commands automatically. Finally, I tried the fourth method, cd /data/shell and directly enter hello.sh, or the source of hello.sh.脚本也可以用这两种方法执行。
It looks like they both run the script directly in the current shell. Wait, one more thing, I noticed that the result of executing the script is not returned to my command line.这是为什么

如何在shell脚本中,判断一个基本命令执行是否成功

说白了,连接Linux主机并使用Telnet命令的过程其实很简单,但很容易陷入困境。
我们先来说说最重要的事情。
After connecting to the Linux host, enter "Telnet 1 9 2 .1 6 8 .1 2 0.2 09 " directly on the command line and press Enter. There's a key detail here. If the connection fails, the port may not be open or the firewall may be blocking it.一开始以为只是网络问题,后来发现不对劲,必须检查Linux主机上的Telnet服务是否启动。

Another point is that the Linux system shell is the interface between the user and the kernel. It is responsible for interpreting commands entered by the user and passing them to the kernel.比如我们去年跑的项目,大概有3 000个大小,由于shell配置错误导致命令执行失败,花了几天时间才解决。

Wait, one more thing, the location of the Shell is usually in the /bin or /usr/bin directory. You can use "cat /etc/shell" to view available shells on your system. The default SHELL can be viewed via the "echo $SHELL" command. The Echo command is used to generate a string, remember to enclose it in double quotes.
最后,如果遇到提示格式不正确的问题,可以使用“echo $PS1 ”和“echo $PS2 ”查看当前格式或更改。
As for the shell script, use "vitest" to enter the script editing page, and the script name is "test.sh".
Practical Tips: Before running the Telnet command, make sure the Telnet service on the Linux host is active and check the firewall settings. Many people don't pay attention to this point, but it is really a trap.你怎么认为?