我是shell脚本的新手,请你帮忙解决以下问题,谢谢.
$AU_NAME=AU_MSM3-3.7-00.01.02.03
#separate the string after last "-", with "." as delimiter
#that is, separate "00.01.02.03" and print/save as below.
major=00
minor=01
micro=02
build=03
Run Code Online (Sandbox Code Playgroud) import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-y', '--y-option', action='store_true')
args = parser.parse_args()
y_option = "enable_y" if args.y_option else ''
print(y_option)
Run Code Online (Sandbox Code Playgroud)
如果传入-yor ,我想将变量设置为空字符串,我相信这应该可以通过 argparse 实现,因此不需要该行?我想不通。我无法使用默认值,因为即使未传入,它也会设置默认值。--y-optiony_optionenable_yy_option = ...-y
下面的代码在 ptxdist Makefile 中工作,但想知道是否有更好的解决方案来检查在继续构建之前是否安装了所有必需的软件包?
ENV_VERIFICATION:
@echo ------------START ENV VERIFICATION---------------
if ! dpkg -s sudo | grep Status | grep -q installed; then \
echo ERROR: sudo package not installed!; \
exit 1; \
fi
if ! dpkg -s scons | grep Status | grep -q installed; then \
echo scons package not installed!; \
exit 1; \
fi
@echo ------------END ENV VERIFICATION---------------
Run Code Online (Sandbox Code Playgroud)
我可以在我的系统中运行以下命令,最好在 make 日志中打印相同的命令,任何有助于改进上述代码并在日志中打印以下输出(如果安装了包)的帮助都是值得赞赏的。提前致谢!
$ dpkg-query -W -f='${Package} ${Status}\n' sudo
sudo install ok installed
Run Code Online (Sandbox Code Playgroud) 文件“测试”的内容:
[]# 0-CPU4 8.9%, 9336/832, 0x5ffe9b88--0x5ffec000
[]# 0-CPU0 13.5%, aa: 4/3, xvl: 35
[]# 0-CPU1 8.6%, SM: 1/4, ovl: 60
[]# 0-CPU0 38.8%, SM: 1/4, ovl: 62
Run Code Online (Sandbox Code Playgroud)
形成这个文件,我想要最后一个CPU0的百分比,即38(忽略小数点)
我使用下面的 shell 命令,效果很好,想知道是否有更好的方法。
grep CPU0 test | tail -1 | awk '/0-CPU0/ {print $3}' | sed 's/\..*//'
#above command prints "38"
Run Code Online (Sandbox Code Playgroud) git status --ignore-submodules=all
Run Code Online (Sandbox Code Playgroud)
我正在寻找与此相反,有没有办法只打印子模块更改?喜欢git status --show-only-submodules
编辑:我的脚本获取并合并子模块更改,然后获取并合并超级项目更改。现在,我需要检查新引入的超级项目更改是否包括子模块更改的SHA更新,为此我想git show HEAD^ HEAD在超级项目中执行此操作,如下所示:
--- a/xyz
+++ b/xyz
@@ -1 +1 @@
-Subproject commit f991877822a8fd50b90f53894f2592a852039547
+Subproject commit f57c051f91b0948a5b8947430b516e650b50348e
Run Code Online (Sandbox Code Playgroud)
从这里,我将进入+Subproject commit并进入xyz目录,执行a git log -1 --no-merges并比较SHA,如果匹配,那么我将假定超级更改包括已引入的子模块更改。
因此,在超级项目中时,我只想列出子模块的更改git status --some-option。请让我知道是否有更好的方法可以做到这一点。
我在下面的try-except中捕获JSON解析错误:
with open(json_file) as j:
try:
json_config = json.load(j)
except ValueError as e:
raise Exception('Invalid json: {}'.format(e))
Run Code Online (Sandbox Code Playgroud)
为什么要During handling of the above exception, another exception occurred打印出来,我该如何解决?
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 103 column 9 (char 1093)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
<....>
raise Exception('Invalid json: {}'.format(e))
Exception: Invalid json: Expecting ',' delimiter: line 103 column 9 (char 1093)
Run Code Online (Sandbox Code Playgroud) 我有如下声明式管道,它并行运行 2* 和 3* 阶段,粘贴下面的蓝海图。
pipeline {
agent { label 'my_node' }
options {
timestamps()
parallelsAlwaysFailFast()
}
stages {
stage('1') {
steps {
script {
step([$class: 'WsCleanup'])
}
}
}
stage('2') {
parallel {
stage("2.1") {
steps {
script {
sh 'echo hi 2.1'
}
}
}
stage("2p") {
steps {
script {
sh 'echo hi 2p'
}
}
}
}
}
stage('3') {
parallel {
stage('3.1') {
steps {
script {
sh """
echo hi 3.1
"""
}
} …Run Code Online (Sandbox Code Playgroud) 以下脚本使用该-e选项运行,因此如果其中的任何命令失败,它将退出:
#!/bin/sh -e
command1 #script should fail if command1 fails
command2 #script should NOT fail if command2 fails
command3 #script should fail if command3 fails
Run Code Online (Sandbox Code Playgroud)
如何让脚本不失败command2?
在Windows上,我想只在脚本死亡时执行某些操作.下面的块没有帮助; 我认为这是因为Windows不支持信号.
$SIG{__DIE__} = sub {
qx(taskkill /F /IM telnet.exe);
CORE::die @_;
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试了这个:
END {
qx(taskkill /F /IM telnet.exe);
exit $exit_code;
}
Run Code Online (Sandbox Code Playgroud)
它执行了taskkill,但退出了退出代码0.我需要传播,exit_code因为我们基于它进行进一步处理.
这些是源文件标题中的有效年份,版权行:
2016
2007,2016
2007-2010,2016
2010,2012-2016
Run Code Online (Sandbox Code Playgroud)
本年度(2016年)应该是其中的一部分.
无效:
2016,2007 # not in order
2010-2007,2016 #range not in order.
2010-2015 # current year missing.
Run Code Online (Sandbox Code Playgroud)
我制作了如下脚本,仍然在努力,是否有更好的方法来检查相同的?
use strict;
use warnings;
use List::Util 'first';
use Time::Piece;
my $t = Time::Piece->new();
my $currentYear=$t->year;
my $filename="sample.txt"; # this file name will be passed-in to the script.
my $fileContent=`cat $filename`;
my $copyrightFound = first { /copyright .* Shakespeare/i } $fileContent;
if (!$copyrightFound) {
print "\nERROR: Copyright missing\n";
exit;
}
#copyright Found
print $fileContent;
if ($fileContent =~ /Copyright …Run Code Online (Sandbox Code Playgroud) linux ×3
shell ×3
perl ×2
python ×2
git ×1
gnu-make ×1
jenkins ×1
jenkins-declarative-pipeline ×1
python-3.6 ×1
python-3.x ×1
regex ×1
sh ×1