#!/bin/sh while true; do if [ -e /tmp/4G.error ]; then echo "Found /tmp/4G.error, calling net4g_auto" net4g_auto break fi sleep 1 done 把上述脚本改成do while
时间: 2024-02-19 21:58:39 浏览: 34
在 Shell 脚本中没有 `do while` 语法,可以使用 `while true; do ... done` 来代替。不过如果一定要使用 `do while` 的语法,可以使用以下的方法实现:
```
#!/bin/sh
found_error=false
while true; do
if [ -e /tmp/4G.error ]; then
found_error=true
break
fi
sleep 1
done
if $found_error; then
echo "Found /tmp/4G.error, calling net4g_auto"
net4g_auto
fi
```
这个脚本会先定义一个变量 `found_error` 并设置为 `false`。然后使用 `do while` 的语法来循环,检查 `/tmp/4G.error` 文件是否存在。如果存在就将 `found_error` 设置为 `true` 并跳出循环。循环结束后,再根据 `found_error` 的值来判断是否需要执行 `net4g_auto` 命令。
相关问题
# !/bin/sh 和# !/bin/bash
#!/bin/sh 和 #!/bin/bash 是脚本文件中的特殊注释行,用于指定解释器执行脚本。
#!/bin/sh 是指定使用默认的shell解释器来执行脚本,通常是Bourne shell或其兼容版本。
而#!/bin/bash 则指定使用Bash shell来执行脚本。
在引用中,将mybash_1.sh的第一行改为#!/bin/sh,然后执行脚本时,会使用默认的shell解释器执行脚本。而在引用和引用中,将脚本的第一行改为#!/bin/bash,这样执行脚本时会使用Bash shell来解释执行脚本。
需要注意的是,在引用中提到的/bin/sh相当于/bin/bash --posix,这意味着将使用Bash shell的posix模式来解释执行脚本。而在引用中提到,当执行完source命令时并没有继续执行,这是因为脚本中的#!/bin/bash这一行只是一个注释,并没有起到指定解释器的作用。
所以,使用不同的注释行可以指定不同的shell解释器来执行脚本,这也会影响脚本的执行结果和行为。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [#!/bin/bash和#!/bin/sh区别](https://blog.csdn.net/qianniuwei321/article/details/126428896)[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_2"}}] [.reference_item style="max-width: 50%"]
- *2* [关于#!/bin/bash和#!/bin/sh详细解析](https://blog.csdn.net/ankang654321/article/details/88698880)[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_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
shell脚本开头的#!/bin/sh 和#!/bin/ksh 会有什么区别
在shell脚本开头,#!后面的内容指定了脚本使用的解释器。#!/bin/sh表示使用Bourne shell,而#!/bin/ksh表示使用Korn shell。两者的区别在于Korn shell比Bourne shell更加强大,支持更多的特性和语法。因此,如果脚本需要使用Korn shell的特性,那么就应该使用#!/bin/ksh。否则,如果脚本只需要使用Bourne shell的特性,那么就可以使用#!/bin/sh。
举个例子,如果脚本需要使用Korn shell的数组特性,那么就应该使用#!/bin/ksh。如下所示:
```shell
#!/bin/ksh
my_array=(1 2 3 4 5)
echo ${my_array[2]} # 输出:3
```
如果脚本只需要使用Bourne shell的基本特性,那么就可以使用#!/bin/sh。如下所示:
```shell
#!/bin/sh
echo "Hello, world!" # 输出:Hello, world!
```