这是我到目前为止所拥有的:
#!/bin/bash
for file in $PATH ; do # Scanning files in $PATH
if [ -x ] ; then #Check if executable
echo "Executable File"
else
echo "Not executable"
fi
done
Run Code Online (Sandbox Code Playgroud)
输出是“文件是可执行的”
我认为它没有正确循环所有文件夹
我经常使用这种模式在 GitHub 中执行原始版本的远程 Bash 脚本:
wget -O - https://raw.githubusercontent.com/<username>/<project>/<branch>/<path>/<file> | bash
Run Code Online (Sandbox Code Playgroud)
通常我可以毫无问题地做到这一点,但是由于我将以下代码添加到某个脚本中,我得到了一个无限循环echo(即使我只是将它从 GitHub 复制粘贴到终端并直接执行,它也经常发生):
wget -O - https://raw.githubusercontent.com/<username>/<project>/<branch>/<path>/<file> | bash
Run Code Online (Sandbox Code Playgroud)
我至少可以部分解决这个问题,例如:
while true; do
read -p "Example question: Do you wish to edit the PHP file now?" yn
case $yn in
[Yy]* ) nano PROJECT/PHP_FILE; break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no.";;
esac
done
Run Code Online (Sandbox Code Playgroud)
这表明| bash管道至少会使问题恶化,因为问题总是伴随着它发生。
为什么会echo "Please answer yes or no."“无休止地”发生?(我用CTRLC+停止它C)
您在单行执行命令和/或 …
我想执行commandA打印以下内容:
line1
line2
line3
started successfully
line4
...
...
Run Code Online (Sandbox Code Playgroud)
当打印该行时,started successfully我想启动另一个命令(并且可能多路复用它们的输出,以便两者都打印在终端上)。
我怎样才能在 bash 中实现这一目标?
我想做这样的事情,但它不会在管道结束后保存变量:
fs=( )
echo ${fs[@]}
ls -A1 |
while read f
do
echo ${fs[@]}
fs+=( "$f" )
echo ${fs[@]}
done
echo "All files/dirs: "${fs[@]}
Run Code Online (Sandbox Code Playgroud)
对于当前目录中的文件 1、2 和 3,我得到以下输出:
# Output:
1
1
1 2
1 2
1 2 3
All files/dirs:
Run Code Online (Sandbox Code Playgroud)
管道结束后,如何保持 fs 变量的值?
更新:
Shell:同时中断 if 和 for 循环。
我的脚本:ip 来命名解析代码
IP=192.168.27.191
hostNameChecker()
{
if [ `getent hosts $1 | wc -l` -ne 0 ];then
HOST_NAME=`hostname`
DNS_IP=`getent hosts $1 | cut -d " " -f 1 | sed 's/^[ \t]*//;s/[ \t]*$//'`
DNS_NAMES=`getent hosts $1 | cut -d " " -f 2- | sed 's/^[ \t]*//;s/[ \t]*$//'`
for DNS_NAME in ${DNS_NAMES[@]}
do
if [[ "$DNS_NAME" == "$HOST_NAME" ]];then
echo "Host name:$HOST_NAME and DNS_NAME:$DNS_NAME matched"
break 2;########Not working. only brakes inner for not outer if
else …Run Code Online (Sandbox Code Playgroud) 我有一个 txt 文件,其中包含我需要分析的所有文件名。我有这个文件(inputFile.txt):
/path/file1a path/file1b
/path/file2a path/file2b
Run Code Online (Sandbox Code Playgroud)
我有一个代码需要两个两个输入文件才能运行。
./Analyzethis /path/file1a /path/file1b
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建一个 bash 循环来遍历我的 txt 文件,读取每一行并运行我的代码。
我有
#!/bin/bash
IFS=$'\n'; for FILE1 in `cat inputFile.txt`; do ./Analyzethis $FILE1; done
Run Code Online (Sandbox Code Playgroud)
但是,它不起作用。我如何循环遍历每一行并将列与 bash 循环一起使用?
我认为我的问题与此类似,但有一个额外的步骤 - 假设我有 3 个脚本:
我该如何做这样的伪代码:
if (main_script.sh finished without error):
run report_success.sh
else:
run report_failure.sh
Run Code Online (Sandbox Code Playgroud)
?
谢谢!
这是我的脚本:
#!/bin/ksh
#this is where I want to go again if user enter
#an answer other than "yes or no"
echo "yes or no?"
read ans
case $ans in
[yY]*)
echo "yes"
;;
[nN]*)
echo "no"
;;
*)
echo "yes or no only"
# here, if the answer is not "Y" or "N",
# I want to go back to asking "yes or no?"
;;
esac
Run Code Online (Sandbox Code Playgroud)
谁能给我一个提示?
我真正喜欢的 Perl 的一个特性是它将循环控制关键字泛化到任何花括号分隔的词法块1。
例如,可以使用 Perl 的last指令退出任何此类块,如下面的玩具脚本所示:
#!/usr/bin/env perl
use strict;
my $value = $ARGV[0] || 0;
my $overall_status = 'failed';
{
$value > 0 and print "$value > 0\n" or last;
$value > 1 and print "$value > 1\n" or last;
$value > 2 and print "$value > 2\n" or last;
$value > 3 and print "$value > 3\n" or last;
$overall_status = 'ok';
}
die 'EARLY EXIT' unless $overall_status eq 'ok';
print "ok\n"; …Run Code Online (Sandbox Code Playgroud) 我使用以下代码,这是我用来更新我的 WordPress 网站的脚本的一部分:
#!/bin/bash
drt="/var/www/html"
for dir in ${drt}/*/; do
if pushd "$dir"; then
wp plugin update --all --allow-root
wp core update --allow-root
wp language core update --allow-root
wp theme update --all --allow-root
popd
fi
done
Run Code Online (Sandbox Code Playgroud)
我学到的具体的pushd-popd当我搜索一种方式来更新我的所有WordPress的情况下一气呵成在此代码中使用的模式。
我不清楚为什么这段代码包含一个if-fi段。
我可以以某种方式更改语法,以便我拥有基本相同的模式但没有if-fi段吗?
例如,而不是这样:
if pushd "$dir";
popd
commands
fi
Run Code Online (Sandbox Code Playgroud)
我会有这样的伪代码:
pushd "$dir";
commands
popd
Run Code Online (Sandbox Code Playgroud)
我可以想象我们如何在没有if-fi语句(伪代码)的情况下告诉计算机这样的事情:
for dir in ${drt}/*; do pushd "$drt"; then
commands
popd
Run Code Online (Sandbox Code Playgroud)
您可能希望在答案中包含不同的方法(即根本没有pushd- …