相关疑难解决方法(0)

在while循环内修改的变量不会被记住

在下面的程序中,如果我$foo在第一个if语句中将变量设置为值1 ,那么它的作用就是在if语句之后记住它的值.但是,当我将同一个变量设置ifwhile语句内部的值2时,它在while循环之后就被遗忘了.它的行为就像我$foowhile循环中使用某种变量的副本而我只修改那个特定的副本.这是一个完整的测试程序:

#!/bin/bash

set -e
set -u 
foo=0
bar="hello"  
if [[ "$bar" == "hello" ]]
then
    foo=1
    echo "Setting \$foo to 1: $foo"
fi

echo "Variable \$foo after if statement: $foo"   
lines="first line\nsecond line\nthird line" 
echo -e $lines | while read line
do
    if [[ "$line" == "second line" ]]
    then
    foo=2
    echo "Variable \$foo updated to $foo inside if inside while loop"
    fi
    echo "Value …
Run Code Online (Sandbox Code Playgroud)

bash scope sh while-loop

168
推荐指数
3
解决办法
15万
查看次数

如何在commit-msg挂钩中提示用户?

我想警告用户,如果他们的提交消息不遵循某组指南,然后给他们选项来编辑他们的提交消息,忽略警告或取消提交.问题是我似乎无法访问stdin.

下面是我的commit-msg文件:

function verify_info {
    if [ -z "$(grep '$2:.*[a-zA-Z]' $1)" ]
    then
        echo >&2 $2 information should not be omitted
        local_editor=`git config --get core.editor`
        if [ -z "${local_editor}" ]
        then
            local_editor=${EDITOR}
        fi
        echo "Do you want to"
        select CHOICE in "edit the commit message" "ignore this warning" "cancel the commit"; do
            case ${CHOICE} in
                i*) echo "Warning ignored"
                    ;;
                e*) ${local_editor} $1
                    verify_info "$1" $2
                    ;;
                *)  echo "CHOICE = ${CHOICE}"
                    exit 1
                    ;;
            esac
        done
    fi
}

verify_info …
Run Code Online (Sandbox Code Playgroud)

git hook commit-message

69
推荐指数
3
解决办法
2万
查看次数

当文件已重定向到stdin时,读取stdin以获取用户输入

所以我想尝试做以下事情:

while read line; do
    read userInput
    echo "$line $userInput"
done < file.txt
Run Code Online (Sandbox Code Playgroud)

所以说file.txt有:

Hello?
Goodbye!
Run Code Online (Sandbox Code Playgroud)

运行该程序将创建:

Hello?
James
Hello? James
Goodbye!
Farewell
Goodbye! Farewell
Run Code Online (Sandbox Code Playgroud)

问题(自然地)成为用户输入读取从stdin读取,在我们的例子中是文件.txt.有没有办法改变它从临时读取到终端的位置以获取用户输入?

注意:我正在使用的文件长度为200,000行.每条线长约500个字符.因此,如果需要,请记住这一点

bash

15
推荐指数
1
解决办法
1万
查看次数

Bash读取用户y/n应答不起作用(在循环读取查找输出时读取命令)

我这里有问题.好像我的Bash脚本忽略了do和之间的所有内容done.不知道为什么,也许你会看到问题.提前致谢.

katalogas=$1
find $katalogas -type f -mtime +3 | while read $failai
do
read -p "Run command $foo? [yn]" answer
if [[ $answer = y ]] ; then
  rm $failai
fi
done
Run Code Online (Sandbox Code Playgroud)

linux bash

5
推荐指数
1
解决办法
2953
查看次数

在 while 循环中等待按键并停止脚本

我想等待按键并在按下字母时退出q。该脚本不等待密钥。如何纠正呢?

while read line
do
    ...
    while :
    do
        read -n 1 key

        if [[ $key = q ]]
        then
            break
        fi
    done
done < $1
Run Code Online (Sandbox Code Playgroud)

bash

5
推荐指数
1
解决办法
6111
查看次数

如何将stdin复制到两个文件中?

我需要script.sh,这将创建文件f1.txtf2.txt发送到stdin的内容.例如:

echo ABRACODABRA | script.sh
Run Code Online (Sandbox Code Playgroud)

...应该创建文件f1.txtf2.txt内容ABRACODABRA.

我怎样才能做到这一点?

请提供script.sh身体!

unix io bash

4
推荐指数
1
解决办法
4701
查看次数

标签 统计

bash ×5

commit-message ×1

git ×1

hook ×1

io ×1

linux ×1

scope ×1

sh ×1

unix ×1

while-loop ×1