sh 运行shell
时间: 2023-07-03 18:25:28 浏览: 45
在 Linux 系统中,可以使用 sh 命令来运行 Shell 脚本。sh 是一个 shell 程序,可以解释执行 Shell 脚本。通常 Shell 脚本的文件扩展名为 .sh,例如 test.sh。
要运行一个 Shell 脚本,可以在终端中输入以下命令:
```
sh test.sh
```
其中,test.sh 是要运行的 Shell 脚本文件名。如果没有在文件名前加上 ./,则系统会在 PATH 环境变量所列出的路径中查找该文件。
另外,还可以使用以下命令来运行 Shell 脚本:
```
./test.sh
```
这种方式需要给脚本文件添加可执行权限,即执行以下命令:
```
chmod +x test.sh
```
这样,就可以直接通过 ./test.sh 命令来运行 Shell 脚本了。
相关问题
c语言运行shell脚本
在C语言中,可以使用system()函数或popen()函数来运行shell脚本。
1. 使用system()函数运行shell脚本:
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
int result = system("shell_script.sh"); // 替换为你的shell脚本文件名
if (result == -1) {
printf("运行shell脚本失败\n");
} else {
printf("运行shell脚本成功\n");
}
return 0;
}
```
2. 使用popen()函数运行shell脚本并获取输出结果:
```c
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
char buffer[1024];
fp = popen("shell_script.sh", "r"); // 替换为你的shell脚本文件名
if (fp == NULL) {
printf("打开shell脚本失败\n");
return -1;
}
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
printf("%s", buffer);
}
pclose(fp);
return 0;
}
```
这两种方法都可以用来运行shell脚本,但是system()函数只返回命令是否运行成功的标志位,而popen()函数可以获取shell指令的运行结果。
shell命令运行.SH文件
要运行一个.sh文件,你可以使用以下命令:
sh ./test.sh
这个命令会执行名为test.sh的脚本文件。其中,sh是指调用shell解释器,而./test.sh是指当前目录下的test.sh文件。
在Linux中,有多种不同的Shell可用,如Bourne Shell、Bourne Again Shell、C Shell、K Shell等。不同的Shell有不同的特点和用途。例如,Bourne Shell的路径是/usr/bin/sh或/bin/sh,而Bourne Again Shell的路径是/bin/bash。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *2* *3* [【Linux中shell命令】.sh文件种种操作](https://blog.csdn.net/a_piece_of_ppx/article/details/124929728)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"]
[ .reference_list ]