简体   繁体   English

Bash 脚本中的箭头键产生奇怪的字母

[英]Arrow keys inside Bash script produce weird letters

I wrote a simple script to manage the containers inside my application, however, whenever I press the arrow keys, they produce something like [[A or other characters.我编写了一个简单的脚本来管理我的应用程序中的容器,但是,每当我按下箭头键时,它们就会产生类似[[A或其他字符。 How can I fix the issue?我该如何解决这个问题?

Example command is ./service.sh app shell which will open up the shell of my Laravel application inside Docker container.示例命令是./service.sh app shell它将打开 shell 我的 Laravel 应用程序在 Z22345FD214CDDB0D2B 容器内的 shell。

#!/usr/bin/env bash

#application=$(basename "$PWD")
application="plexmediamanager"

services=("app", "roadrunner", "scheduler", "frontend", "nginx", "redis", "database", "queue", "torrent", "jackett", "traefik")
actions=("start", "stop", "restart", "shell", "rebuild")

if [[ ! "${services[@]}" =~ "${1}" ]]; then
    echo "Unknown service: ${1}"
    printf -v servicesString "%s" "${services[@]}"
    echo "Available services: ${servicesString}"
    exit 0
fi

if [[ ! "${actions[@]}" =~ "${2}" ]]; then
    echo "Unknown action: ${2}"
    echo "Available actions: start|stop|restart|shell|rebuild"
    exit 0
fi

case "$2" in
    "start")
    docker-compose start $1
    ;;
    "stop")
    docker-compose stop $1
    ;;
    "restart")
    docker-compose restart $1
    ;;
    "shell")
    docker exec -it "${application}_${1}" /bin/sh
    ;;
    "rebuild")
    docker-compose stop $1
    docker-compose build $1
    docker-compose start $1
    ;;
esac

As mentioned in comments, the shell you call in your case statement does not exit and has no functionality for scrolling through history as you are used to in bash.如评论中所述,您在case语句中调用的 shell 不会退出,并且没有像您在 bash 中习惯的那样滚动历史记录的功能。 You may be able to get that shell to exit using something like this:您可以使用以下方式让 shell 退出:

docker exec -it "${application}_${1} && exit" /bin/sh

But that's just an untested guess.但这只是一个未经检验的猜测。

Of course if you leave off the call to the other shell (and let the script run that command in the parent shell), you could keep your script awaiting new command input by allowing for user input via read and stowing that inside a while loop.当然,如果您停止对另一个 shell 的调用(并让脚本在父 shell 中运行该命令),您可以通过允许用户通过read输入并将其存放在while循环中来让脚本等待新的命令输入。

(Also, it should be noted that you are handing exec two user-supplied variables and that is not a good practice. Avoid exec , generally.) (另外,应该注意的是,您正在向exec提供两个用户提供的变量,这不是一个好习惯。通常避免exec 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM