-1

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
    Inside the script is a function. You run the script and it defines the function, but it never runs it. Call the function at the bottom of the script with the line `installDependencies` – JNevill Jun 29 '23 at 14:54
  • 2
    Where do you define `TICK`? Your output looks like it shows the echo (e.g. `file-5.38 File installed:`). Is that real output? Also make sure `TICK` does not contain DOS line endings, the fact that file-5.38 comes before File installed is suspicious. – tjm3772 Jun 29 '23 at 14:55
  • See: [Are shell scripts sensitive to encoding and line endings?](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) – tjm3772 Jun 29 '23 at 14:58
  • 1
    As another note, `echo -e` [is bad practice; don't use it](https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo). Even the [POSIX standard for `echo`](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html) says to use `printf` instead when you might need backslash escapes (the scenario where bash's default `echo -e` behavior is called for -- note that it also has non-default behavior that you almost certainly don't want, read the first link for details) – Charles Duffy Jun 29 '23 at 15:15
  • 1
    `which curl | grep -c 'bin'` Why are you doing that? Do you _care_ if curl is in path containing bin? – KamilCuk Jun 29 '23 at 16:00

2 Answers2

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.

Ljm Dullaart
  • 4,273
  • 2
  • 14
  • 31
  • 2
    Why use `which`? `if command -v curl >/dev/null 2>&1; then` will work even without `which` (a nonstandard, non-POSIX tool) installed, and a bit more efficiently (as a shell builtin, no need to fork a subprocess and invoke an external executable). – Charles Duffy Jun 29 '23 at 15:12
  • 1
    Also, this doesn't solve the problem where the OP _never actually starts_ the function that they define. – Charles Duffy Jun 29 '23 at 15:14
  • 1
    @CharlesDuffy: Because the OP used `which`. And it is not absolutely wrong. `command -v` is just better. – Ljm Dullaart Jun 29 '23 at 15:28
  • 1
    Prefer builtin commands, like `command` or `hash` over external `which`. – KamilCuk Jun 29 '23 at 16:00
0

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
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • I wish there was a Direct reply to post's would help answer the questions I'm getting, as well as learn from you guys. Like I said I'm new to Linux and from all the engineers i have met in this space they seem to want to teach. – Scott Johnstone Jun 30 '23 at 08:37