Bash脚本echo Shebang

huangapple go评论47阅读模式
英文:

Bash script echo Shebang

问题

是的,我知道这个问题以前被提过,我一直在寻找答案。在Linux方面,我是个新手,所以请听我说。

在WSL1上的Ubuntu 20.04中,我映射了我的C:驱动器。

我有一个c:\test.sh文件,其中包含以下代码:

#!/bin/bash
installDependencies()
{
# 这里最好使用ForEach或For循环
    if [ "$(which curl | grep -c 'bin')" -ne 0 ]; then
        curl --version | head -n 1;
        echo -e "Curl已安装:$TICK"
    fi

    if [ "$(which file | grep -c 'bin')" -ne 0 ]; then
        file --version | head -n 1;
        echo -e "File已安装:$TICK"
    fi
}

然后运行以下命令:

/mnt/c/$ type ./test.sh

它运行了,但没有显示echo的内容。我漏掉了什么?

看起来我要么漏掉了什么,要么人们有一些我没有考虑的假设。

提前感谢您的回答。

curl 7.68.0 (x86_64-pc-linux-gnu) libcurl/7.68.0 OpenSSL/1.1.1f
zlib/1.2.11 brotli/1.0.7 libidn2/2.2.0 libpsl/0.21.0 (+libidn2/2.2.0)
libssh/0.9.3/openssl/zlib nghttp2/1.40.0 librtmp/2.3 Curl installed:

file-5.38 File installed:

更新-----

嗨,这是我一直在尝试拆分的现有脚本。我以为拆分成部分对我来说会更容易。

但是阅读过程中,一些事情让我意识到installDependencies()类似于一个函数。因此,在调用它之前,它什么都不会做。

对不起,这是个错误,而且不是我的第一个错误,也不会是最后一个。感谢您的回应和脑海中的提醒。感谢所有回应新手的人。

tick变量在脚本的下面。

# 设置变量
TICK="\e[32m✔\e[0m"
CROSS="\e[31m✘\e[0m"
WARNING="\e[33m‼\e[0m"

更新---------

# 让我们开始吧
echo "好的,我们开始吧... 🙈"

echo "==================================================================="
installDependencies
sleep 10
echo "==================================================================="

我认为依赖项和已安装组件只是输出是否安装了某些内容。

我们还有其他组件,然后安装像HomeBrew这样的东西,这也是函数。

echo "==================================================================="
installHomeBrew
sleep 10
echo "==================================================================="

再次感谢所有的帮助,我将考虑用printf替换echo,并开始研究/dev/null,使脚本更加统一。

英文:

Yes I know this have been asked before and I have been looking. Im a newbie when it comes to linux so here goes.

WSL1 ubuntu 20.04
Mapped drive to my C:drive

I have a c:\test.sh
code

#!/bin/bash
installDependencies()
{
# TODO ForEach or For loop would be better here
    if [ "$(which curl | grep -c 'bin')" -ne 0 ]; then
        curl --version | head -n 1;
        echo -e "Curl installed: $TICK"
    fi

    if [ "$(which file | grep -c 'bin')" -ne 0 ]; then
        file --version | head -n 1;
        echo -e "File installed: $TICK"
    fi
}
/mnt/c/$ type ./test.sh 

It runs but does not show the echo's What am I missing?

looking at the threads seems that I'm either missing something or people have assumptions that I am missing.

Cheers in advanced.

> curl 7.68.0 (x86_64-pc-linux-gnu) libcurl/7.68.0 OpenSSL/1.1.1f
> zlib/1.2.11 brotli/1.0.7 libidn2/2.2.0 libpsl/0.21.0 (+libidn2/2.2.0)
> libssh/0.9.3/openssl/zlib nghttp2/1.40.0 librtmp/2.3 Curl installed:
>
> file-5.38 File installed:

Update-----

Hi this is an existing Script that I have been trying to break down. I thought it would be easier for me to break down sections.

But reading has just pointed somethings out to me such as installDependencies() is similar to a function. So it will do nothing until its called.

Apologies what a week, and I was looking way to closely at this. Its not my first mistake and it wont be my last. Appreciate the response and for the brain nudge. For all that responded to the newbie thanks.

The tick variables was further down the script.

# Setting variables
TICK="\e[32m✔\e[0m"
CROSS="\e[31m✘\e[0m"
WARNING="\e[33m‼\e[0m"

Update---------

# Let's get started
echo "Ok, here we go... 🙈"

echo "==================================================================="
installDependencies
sleep 10
echo "==================================================================="

I think the dependences and installed components are to just output if something is installed.

We have other components that then install things like HomeBrew with are also functions.

echo "==================================================================="
installHomeBrew
sleep 10
echo "==================================================================="

Again thanks for all the help will look at replacing echo with printf. As well as starting to look at dev/null and making the script a bit more uniform.

答案1

得分: 1

作为回复,因为要详细解释一下评论。

要测试你的 if 条件是否失败,请使用 else 子句:

#!/bin/bash
installDependencies()
{
# TODO 这里使用 ForEach 或 For 循环会更好
    if [ "$(which curl | grep -c 'bin')" -ne 0 ]; then
        curl --version | head -n 1;
        echo -e "Curl 安装成功: $TICK"
    else
        echo "Curl 未安装在 bin 目录中"
    fi

    if [ "$(which file | grep -c 'bin')" -ne 0 ]; then
        file --version | head -n 1;
        echo -e "File 安装成功: $TICK"
    else
        echo "File 未安装在 bin 目录中"
    fi
}
installDependencies  # 请参阅下面的额外注释

请注意,你的脚本只定义了函数 installDependencies。如果要执行它,你需要实际调用它。

你正在发出 type test.sh 命令,然后脚本不会被执行。type 用于 "显示有关命令类型的信息"。请查看 type --help

不清楚为什么你要如此复杂地测试 curl 的存在,当以下代码足够了,除非你确实希望它安装在 bin 目录中:

if which curl > /dev/null ; then

你从未为变量 TICK 赋值,所以 $TICK 将始终为空。

英文:

As a reply, because to elaborate for a comment.
To test if your if conditions are failing use else clause:

#!/bin/bash
installDependencies()
{
# TODO ForEach or For loop would be better here
    if [ "$(which curl | grep -c 'bin')" -ne 0 ]; then
        curl --version | head -n 1;
        echo -e "Curl installed: $TICK"
    else
        echo "Curl not installed in a bin directory"
    fi

    if [ "$(which file | grep -c 'bin')" -ne 0 ]; then
        file --version | head -n 1;
        echo -e "File installed: $TICK"
    else
        echo "File not installed in a bin directory"
    fi
}
installDependencies  # See additional comment below

Note that your script only defines the function installDependencies. If you want it to be executed, you need to actually call it.

You are issuing the command type test.sh, then the script is not executed. type is used to "Display information about command type." See type --help.

It is unclear why you do such elaborate test for the existence of curl, when

if which curl > /dev/null ; then

would do just fine, unless you really want it to be installed in a bin directory.

You never assign a value to the variable TICK, so $TICK will always be empty.

答案2

得分: 0

Here's the translated code portion:

#!/bin/bash

TICK="$(tput setaf 2)$(tput sgr0)"

is_installed() {
    type "$1" &>/dev/null
}

installDependencies() {
    # TODO ForEach or For loop would be better here
    if is_installed curl; then
        curl --version | head -n 1;
        printf 'Curl installed: %s\n' "$TICK"
    else
        printf 'Curl not installed\n'
    fi

    if is_installed file; then
        file --version | head -n 1;
        printf 'File installed: %s\n' "$TICK"
    else
        printf 'File not installed\n'
    fi
}
installDependencies

I've translated the code part, and it doesn't include the translation of the comments or other content.

英文:

Perhaps something like this using type and printf:

#!/bin/bash

TICK="$(tput setaf 2)✔$(tput sgr0)"

is_installed() {
    type "$1" &>/dev/null
}

installDependencies()
{
# TODO ForEach or For loop would be better here
    if is_installed curl; then
        curl --version | head -n 1;
        printf 'Curl installed: %s\n' "$TICK"
    else
        printf 'Curl not installed\n'
    fi

    if is_installed file; then
        file --version | head -n 1;
        printf 'File installed: %s\n' "$TICK"
    else
        printf 'File not installed\n'
    fi
}
installDependencies

huangapple
  • 本文由 发表于 2023年6月29日 22:49:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定