skip to Main Content

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.

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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: %sn' "$TICK"
        else
            printf 'Curl not installedn'
        fi
    
        if is_installed file; then
            file --version | head -n 1;
            printf 'File installed: %sn' "$TICK"
        else
            printf 'File not installedn'
        fi
    }
    installDependencies
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search