Grep 通常以红色显示突出显示的文本,这正是我想要的,但这次无论我做什么它似乎都不会这样做。我是从 Docker 容器(nginx:1.19-alpine更具体地说)执行此操作,并使用RUN apk add bash. 然后我添加了一个~/.bashrc包含以下内容的配置文件:
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
force_color_prompt=yes
color_prompt=yes
if [ "$color_prompt" = yes ]; then
PS1=' ${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@server-nginx\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
PS1=' ${debian_chroot:+($debian_chroot)}\u@server-nginx:\w\$ '
fi
unset color_prompt force_color_prompt
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@server-nginx: \w\a\]$PS1"
;;
*)
;;
esac
# enable color support of ls and …Run Code Online (Sandbox Code Playgroud) 如果源变量或目标变量的文件夹名称中有空格,我无法使 rsync 脚本正常工作
#!/bin/bash
HOST="root@192.168.1.100"
source="/path/My Files/"
dest="/path/My Files2/"
rsync -avhP --delete "$HOST":"$source" "$dest"
Run Code Online (Sandbox Code Playgroud)
我得到的错误是
receiving incremental file list
rsync: [sender] link_stat "/path/My" failed: No such file or directory (2)
rsync: [sender] change_dir "/root/Files" failed: No such file or directory (2)
Run Code Online (Sandbox Code Playgroud)
任何关于如何写这个的帮助都会很棒
我有一个如下所示的脚本,该脚本通过在每个循环上重新定义 x 来检查我的 pod 是否处于运行状态。
x=$(/opt/oc get pods --selector app=${bamboo.shortPlanName} -o jsonpath='{range .items[]}{.status.phase}{"\n"}{end}')
until [ "$x" == "Running" ];
do
sleep 5
x=$(/opt/oc get pods --selector app=${bamboo.shortPlanName} -o jsonpath='{range .items[]}{.status.phase}{"\n"}{end}')
echo $x
done
Run Code Online (Sandbox Code Playgroud)
但是,我想修改我当前的脚本以也支持超时,我的意思是循环 60 秒后,应该停止,或者循环 12 次后,应该停止。知道该怎么做吗?
我正在尝试编写一个 shell 脚本,从我的 zsh_history 文件中删除重复的命令。由于没有真正的 shell 脚本经验,并且考虑到我的 C 背景,我写了这个似乎可以工作的怪物(尽管仅在 Mac 上),但需要几辈子才能结束:
#!/bin/sh
history=./.zsh_history
currentLines=$(grep -c '^' $history)
wordToBeSearched=""
currentWord=""
contrastor=0
searchdex=""
echo "Currently handling a grand total of: $currentLines lines. Please stand by..."
while (( $currentLines - $contrastor > 0 ))
do
searchdex=1
wordToBeSearched=$(awk "NR==$currentLines - $contrastor" $history | cut -d ";" -f 2)
echo "$wordToBeSearched A BUSCAR"
while (( $currentLines - $contrastor - $searchdex > 0 ))
do
currentWord=$(awk "NR==$currentLines - $contrastor - $searchdex" $history | cut -d ";" …Run Code Online (Sandbox Code Playgroud) 所以我有一个弹出菜单,我需要使弹出项目在某些条件下消失。为了帮助我发展我的想法和理解,我有一个包含 6 个项目的弹出窗口,其中之一的标题是蓝牙。我在第一个弹出页面中创建了一个名为 ShowParameters 的按钮。我可以使视图模型中的属性为 true 或 false。这似乎工作正常。我已将蓝牙弹出项目的 IsVisible 属性绑定到此 ShowParameters 属性。这不会在单击按钮时更新 IsVisible 属性。似乎 PropertyChanged?.Invoke 未链接到 Flyout 项目,或者 Flyout 项目未链接到该属性,尽管我可以使其在 xaml 本身中出现或消失。显然,作为一个菜鸟,我做了一些非常愚蠢的事情。有人可以指出我有多愚蠢吗;)
AppShell.xaml
<?xml version="1.0" encoding="UTF-8" ?>
<Shell
x:Class="TSDZ2Monitor.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:sys="clr-namespace:System;assembly=netstandard"
xmlns:local="clr-namespace:TSDZ2Monitor"
xmlns:pages="clr-namespace:TSDZ2Monitor.Pages"
Shell.FlyoutBehavior="Flyout"
FlyoutHeaderBehavior="Default"
FlyoutVerticalScrollMode="Auto"
FlyoutBackgroundColor="{StaticResource FlyoutBackgroundColor}">
<Shell.BindingContext>
<local:ShowParametersViewModel/>
</Shell.BindingContext>
<Shell.FlyoutHeaderTemplate>
<DataTemplate>
<Grid BackgroundColor="{StaticResource FlyoutBackgroundColor}"
HeightRequest="200">
<Image
HeightRequest="200"
Source="bicycle.svg"
Margin="10, 10, 10, 10"
Opacity="0.6" />
<Label Text="TSDZ2 Monitor"
TextColor="White"
FontAttributes="Bold" />
</Grid>
</DataTemplate>
</Shell.FlyoutHeaderTemplate>
<Shell.FlyoutFooterTemplate>
<DataTemplate>
<StackLayout>
<Label Text="TSDZ2"
TextColor="GhostWhite"
FontAttributes="Bold"
HorizontalOptions="Center" />
<Label Text="{Binding Source={x:Static sys:DateTime.Now}, StringFormat='{0:MMMM …Run Code Online (Sandbox Code Playgroud) 下面是我的变量声明,在 shell 脚本中,我正在寻找一种方法来循环字符串列表以获取每个项目。
output='['a.txt', 'b.txt', 'c.txt', 'd.txt']'
Run Code Online (Sandbox Code Playgroud)
我尝试了下面的脚本,但它没有按预期工作
for i in "${output}"; do
echo $i
done
Run Code Online (Sandbox Code Playgroud)
预期结果:
a.txt
b.txt
c.txt
d.txt
Run Code Online (Sandbox Code Playgroud) #!/usr/bin/env bash
set -eo pipefail
sha256sum \
Dockerfile-ci \
frontend/pnpm-lock.yaml \
| sha256sum
Run Code Online (Sandbox Code Playgroud)
如果frontend/pnpm-lock.yaml不存在,则运行上面的脚本
sha256sum: frontend/pnpm-lock.yaml: No such file or directory
e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
Run Code Online (Sandbox Code Playgroud)
它正确地记录该文件不存在,但它继续将其传输到下一个sha256sum. 不应该set -eo pipefail立即退出第一个命令的脚本sha256sum而不是通过管道输入第二个命令吗sha256sum?
背景:我试图通过节点脚本顶部的 shebang 在非登录 shell 中正确nvm设置。node
节点脚本的 shebang 是login shell:
#!/usr/bin/env node \nRun Code Online (Sandbox Code Playgroud)\n这不起作用,non-login shell所以我想做相当于:
#!/usr/bin/env [[ -s $HOME/.nvm/nvm.sh ]] && source "$HOME/.nvm/nvm.sh" && node\nRun Code Online (Sandbox Code Playgroud)\n但我无法获得正确的语法(请参阅下面的错误)。我尝试了多种方法,包括引用脚本或尝试运行它bash -c。但每次我都会遇到解析错误。
完整的脚本是:
\n#!/usr/bin/env node\n\nconst semver = require('semver');\nconsole.log("Hello World! You're using " + process.version)\nRun Code Online (Sandbox Code Playgroud)\n错误:
\n在登录 shell 中(我不需要它)
\n\xe2\x9d\xaf bin/nodesample.js\nenv: [[: No such file or directory\nRun Code Online (Sandbox Code Playgroud)\n在非登录外壳中我想找到解决方案
\nenv: [[: No such file or directory\nRun Code Online (Sandbox Code Playgroud)\n … 我想在任何输入字符串中插入点。
例如,输入anystr,并输出所有可能性
a.nystr
an.ystr
any.str
anys.tr
anyst.r
a.n.ystr
a.ny.str
a.nys.tr
a.nyst.r
an.y.str
an.ys.tr
an.yst.r
......
anys.t.r
a.n.y.str
a.n.ys.tr
......
......
a.n.y.s.t.r
Run Code Online (Sandbox Code Playgroud)
插入一个点很容易
a.nystr
an.ystr
any.str
anys.tr
anyst.r
a.n.ystr
a.ny.str
a.nys.tr
a.nyst.r
an.y.str
an.ys.tr
an.yst.r
......
anys.t.r
a.n.y.str
a.n.ys.tr
......
......
a.n.y.s.t.r
Run Code Online (Sandbox Code Playgroud)
但如何循环遍历不同长度的输入字符串的所有可能性?
并不重要 - 但我正在尝试更深入地了解 bash 脚本,这让我发疯!
我的目标 - 在 bash 脚本中:
如果 $args 中没有空格,没问题 - 但这里有一个最小的脚本来说明:
#!/bin/bash
function tstArgs () {
echo "In tstArgs: ArgCnt:$#; Arg1:[$1]; Arg2:[$2]"
}
ARG1=today
ARG2=tomorrow
CMD1="tstArgs $ARG1 $ARG2"
$CMD1 #Output - As Desired: In tstArgs: ArgCnt:2; Arg1:[today]; Arg2:[tomorrow]
ARGWS1="'today with spaces'"
ARGWS2="'tomorrow with spaces'"
CMD2="tstArgs $ARGWS1 $ARGWS2"
$CMD2 #Output: In tstArgs: ArgCnt:6; Arg1:[today]; Arg2:[with]
#The dream:
ARGARR=($ARGWS1 $ARGWS2)
CMD3="tstArgs ${ARGARR[@]}"
$CMD3 #Output: In tstArgs: ArgCnt:6; Arg1:[today]; Arg2:[with]
#ETC, ETC, ETC... …Run Code Online (Sandbox Code Playgroud)