这个脚本运行正常
stat1=$(systemctl list-units --type=service | grep gravity | grep gcr | awk {'print $1'} | xargs systemctl status | grep Active | awk {'print $2'})
echo "$stat1"
if [ $stat1 == 'active' ]
then
echo " >> Gravity service is running ..."
else
echo " >> Gravity service not running ..."
fi
Run Code Online (Sandbox Code Playgroud)
这个不是
stat2=$(gravity status | grep status | awk {'print $3'})
echo "$stat2"
if [ $stat2 == 'active' ]
then
echo " >> Gravity service is running ..."
else …Run Code Online (Sandbox Code Playgroud) 关于Linux Shell脚本有以下内容:
verifyIfFileExists(){
...
returns 0 # if the file exists
...
returns 1 # if the does not exist
}
...
something(){
verifyIfFileExists
resultVerification=$?
if [[ $resultVerification -eq 0 ]]; then
...
else
...
fi
...
}
Run Code Online (Sandbox Code Playgroud)
上面显示的代码按预期工作。我想知道是否可能以及如何在if语句中调用方法和求值 - 它以避免resultVerification=$?声明 - 类似:
something(){
verifyIfFileExists
if [[ $(verifyIfFileExists) -eq 0 ]]; then
...
else
...
fi
Run Code Online (Sandbox Code Playgroud) 假设我的 Bash 有一些广泛的~/.bashrc自定义功能,不仅可以帮助我交互使用,还可以帮助我编写脚本(别名、函数、变量等)。我希望我的所有脚本都可以使用此功能,因此我正在考虑设置BASH_ENV为~/.bashrc(这似乎是做到这一点的方法。)
我的问题是,这有什么问题吗?
~/.bashrc. 但我认为这是一个可以管理的问题,因为无论如何我都会在使用它们之前对其进行审查(或者盲目地相信像 Github 明星这样深奥的统计数据来认为它们是安全的),并且只是偶尔降低自己的水平来取消BASH_ENV任何可能行为不当的脚本。~/.bashrc并没有那么重,如果我想要速度,我就不会使用 shell 脚本。~/.bashrc只会成为我彻底 ganking 的主菜中的温和调味品。~/.bashrc文件分成两个文件,一个用于交互式使用,一个用于脚本编写,并且可能将其设置为交互式文件是脚本文件的超集。但除了那种模糊的纯洁感之外,这听起来似乎是付出了太多的努力却收获太少。~/.bashrc我正在使用的任何功能。这听起来不像是一个 NP 问题。~/.bashrc表明这样的脚本应该首先用另一种语言编写。temp=$1和 和有temp=1什么区别?为什么我需要美元符号?
例如:
#!/bin/bash
temp=$1
cell=$((($temp-32)*5/9))
echo $cell
Run Code Online (Sandbox Code Playgroud) 我需要一个gawk脚本(不是单行的,我需要一个函数)。给定一行整数,即
0 2 4 1 9 5 1 1
Run Code Online (Sandbox Code Playgroud)
该函数应生成连续的前向绝对差,因此:
0 2 4 1 9 5 1 1
2 2 3 8 4 4 0
0 1 5 4 0 4
1 4 1 4 4
3 3 3 0
0 0 3
0 3
3
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点gawk?
#!/bin/bash
set -x
count=0
number=0
loops=0
average=0
read -p " Please enter a number between 1 and 100? " number
while [ $count -lt $number ]
done
average=`expr $score / $number`
echo $average
Run Code Online (Sandbox Code Playgroud)
创建一个 shell 程序,要求用户输入一个 1 到 100 之间的数字,一旦输入了 -1 的标记值,就存在循环。程序必须保留所有循环迭代的计数、总计数以及程序结束后的平均次数。
我猜我必须使用一个if语句,但我不太知道如何使用。
我的意思$yy应该是2019,2020,2021,等。
$yy 如果是的话应该失败 20199 201987
它应该只有 4 位数字。
如何检查这种情况..
yy =$1
if [ $yy > 4 ] ----- something like this
then
echo "Year should be 4 digits only"
fi
Run Code Online (Sandbox Code Playgroud) 我有stdout很多看起来像这样的文本块:
% QUESTION
Who played drums for The Beatles?
% QUESTION
Who played
guitar
for The Beatles?
% QUESTION
Who played
bass for The Beatles
?
Run Code Online (Sandbox Code Playgroud)
这里的想法是将文件分成“块”,其中每个块都以 line 开头% QUESTION。我想编写一个脚本来打印此数据的第 n 个块。
例如,发行nthchunk 3应打印
Who played
bass for The Beatles
?
Run Code Online (Sandbox Code Playgroud)
我怎么能去做这件事?
我已经使用 GNU/Linux 一年多了。Linux 大师们,我需要你回答这个问题:
什么语言(S)都配置文件,如.bashrc,.vimrc,.i3status.conf,.conkyrc,.xinitrc,等用?
考虑以下函数:
function testForBinary {
someBin=$(command -v binary)
# Test if binary is installed
if [[ -n $someBin ]]; then
installSuccess="Success"
echo ${installSuccess}
return
else
# This should never be reached
return 1
fi
}
Run Code Online (Sandbox Code Playgroud)
取自(上下文):
function installAndTestForDialog() {
# dialog allows me to create a pretty installer
# It is a required dependency since XXXXX
# Name Removed as I'm attempting modularization
# of a script from Github that's one huge Script
# See https://unix.stackexchange.com/questions/85249/why-not-use-which-what-to-use-then
local dialogBin …Run Code Online (Sandbox Code Playgroud)