Bash程序奇怪的显示

问题描述 投票:0回答:2

我是bash脚本的新手,我对echo有一个问题:

  1. 好像线条在执行后很久就被打印在屏幕上了
  2. 我错过了“\ n”=>所有都在一个单线上回应:-(

为了保持显示格式,我缺少什么?我错过了qazxsw poi的参数吗?

问题是此后三个脚本示例的最后一行。

useful_functions.是

echo

脚本:

#!/bin/bash
declare -x base_dir="/Users/romain/Downloads"
function download ()
{
# expect one parameter : an url to download
# use wget to download the file
        if [ ! -d $base_dir ]
        then
                echo "ERROR : [$base_dir] is not a directory"
                return 1
        fi
        url=$1
        filename="$(basename $url)"
        target_path="$base_dir/$filename"
        echo ""
        echo "download($url)"

        if [ -e $target_path ]
        then
                echo "\-> $target_path already exists"
        else
                echo "[$filename] doesn't exist"
                echo " \-> Downloading $filename through  $url to $target_path"
                wget $url
                echo "mv [$filename] to [$target_path]"
                mv $filename $target_path
        fi
}
function sha256()
{
# expect two urls as parameters
# first : a file to download
# 2nd : a cheksum file
# download and checksum files
        echo " - sha256 - "
        echo "sha256 ($1, $2)"
        url_fichier=$1
        url_shasum=$2
        download $url_fichier
        download $url_shasum
        filename="$(basename $url_shasum)"
        f_shasum="$base_dir/$filename"
        fichier="$base_dir/$filename"
        echo "=> Verify shasum "
        echo "=> $f_shasum "
        echo "=> $fichier"
        result=$( cd $base_dir ;  shasum -a 256 -c $f_shasum )
        echo " result = [$result]"
        echo $result
}

输出:

source /usr/local/bin/useful_functions.sh
me = $(basename $0)

source /usr/local/bin/useful_functions.sh
destination="/Users/romain/Downloads"
docker=https://download.docker.com/mac/stable/Docker.dmg
shasum=https://download.docker.com/mac/stable/Docker.dmg.sha256sum

clear
echo "executing $0"
echo "coucou"
echo "\-> clearing console"
echo "$docker, $shasum"
echo ""
r=$(sha256  $docker $shasum)
echo $r

加入

executing /usr/local/bin/get_docker.sh
coucou
\-> clearing console
https://download.docker.com/mac/stable/Docker.dmg, https://download.docker.com/mac/stable/Docker.dmg.sha256sum

- sha256 - sha256 (https://download.docker.com/mac/stable/Docker.dmg, https://download.docker.com/mac/stable/Docker.dmg.sha256sum) download(https://download.docker.com/mac/stable/Docker.dmg) \-> /Users/romain/Downloads/Docker.dmg already exists download(https://download.docker.com/mac/stable/Docker.dmg.sha256sum) \-> /Users/romain/Downloads/Docker.dmg.sha256sum already exists => Verify shasum => /Users/romain/Downloads/Docker.dmg.sha256sum => /Users/romain/Downloads/Docker.dmg.sha256sum result = [Docker.dmg: OK] Docker.dmg: OK

我还是得到:

echo -e "That's how use output a newline \n :)"

但我不得不说我在Mac上

bash
2个回答
0
投票

使用-e选项

-e启用反斜杠转义的解释

executing ./get_docker.sh
coucou
\-> clearing console
https://download.docker.com/mac/stable/Docker.dmg, https://download.docker.com/mac/stable/Docker.dmg.sha256sum

- sha256 - sha256 (https://download.docker.com/mac/stable/Docker.dmg, https://download.docker.com/mac/stable/Docker.dmg.sha256sum) download(https://download.docker.com/mac/stable/Docker.dmg) \-> /Users/romain/Downloads/Docker.dmg already exists download(https://download.docker.com/mac/stable/Docker.dmg.sha256sum) \-> /Users/romain/Downloads/Docker.dmg.sha256sum already exists => Verify shasum That's how use output a newline :) => /Users/romain/Downloads/Docker.dmg.sha256sum => /Users/romain/Downloads/Docker.dmg.sha256sum result = [Docker.dmg: OK] Docker.dmg: OK

检查echo -e "That's how use output a newline \n :)" 以获取其他有用的回波参数。


0
投票

好像线条在执行后很久就被打印在屏幕上了

这当然是因为你在“执行它们”时不要让它们被打印,而是你将man pages的整个输出分配给变量sha256并在最后打印所有。

我错过了“\ n”=>所有都在一个单线上回应:-(

问题不在于r,而在于参数扩展,这取决于echo

shell扫描参数扩展,命令替换和算术扩展的结果,这些结果在双引号内没有出现用于分词。

有关这方面的提示,请参阅“Bash参考手册”部分Word Splitting

嵌入的换行不会被删除,但在分词时可能会被删除。

因此,补救措施是引用变量的扩展:

Command Substitution
© www.soinside.com 2019 - 2024. All rights reserved.