我们可以在Bash脚本中使用PHP吗? [英] Can we use PHP in Bash Script?

查看:77
本文介绍了我们可以在Bash脚本中使用PHP吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bash脚本abcd.sh

I have a bash script abcd.sh

#!/bin/sh
for i in `seq 8`; do ssh w$i 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat'; done &
pid=$!
sleep 1
kill -9 $pid

我想在bash脚本中使用PHP.

I want to use PHP in my bash script.

例如:在bash脚本中,我想通过PHP设置seq的值.

eg: in bash script I want to set value of seq through PHP.

推荐答案

嗯,如果您正在使用bash,则应该在第1行使用bash shebang,这样人们就会知道您期望bash功能可用.而且,如果您正在使用bash,则仍然可以使用bash序列:

Mmm, if you are using bash, you should maybe use a bash shebang on line 1 so people know you are expecting bash features to be available. And if you are using bash, you can use a bash sequence anyway:

#!/bin/bash
for i in {1..8}; do echo $i; done

更新1

如果服务器数量是通过PHP获取的,则可以执行以下操作:

If the number of servers is obtained through PHP, you can do something like this:

numservers=$(php -r 'echo 8;')
for i in $(seq $numservers); do echo $i; done
1
2
3
4
5
6
7
8

更新2

好吧,您说服务器的数量是动态的,但是随后您说它是在脚本中设置的(这似乎是矛盾的),但这是您要做的:

Ok, you said the number of servers is dynamic, but then you say it is set in the script (which seems contradictory), but this is what you do:

numservers=10
for i in $(seq $numservers); do echo $i; done
1
2
3
4
5
6
7
8
9
10

这篇关于我们可以在Bash脚本中使用PHP吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆