Jump to content

bash or shell (.sh linux)

Go to solution Solved by Mr_KoKa,

Yup, you're right. It would look like this 

if [ "$#" -ge 1 ]; then
        if [ "$1" == "cmd1" ]; then
                echo "This is cmd 1."
        elif [ "$1" == "cmd2" ]; then
                echo "This is cmd 2."
        else
                echo "$1 is not valid command parameter."
        fi
else
        echo "No command parameter provided."
fi

 

Hi, So I see some people use files that starts diffrent things etc "start parameter" / start server, start webserver etc, But how do I do this? right now I do startwebserver.sh and startserver.sh.

So I want 1 file that can start diffrent things based on the parameter behind it and I don't know how you do that. So any links to places I can learn will help me alot :)

Back-end developer, electronics "hacker"

Link to comment
https://linustechtips.com/topic/639583-bash-or-shell-sh-linux/
Share on other sites

Link to post
Share on other sites

#!/bin/bash

if [ "$#" -ge 1 ]; then
        if [ "$1" == "test" ]; then
                echo "This is a test."
        elif [ "$1" == "say" ]; then
                if [ "$#" -ge 2 ]; then
                        echo "$2"
                else
                        echo "Usage: helper say [thing]"
                fi
        else
                echo "$1 is not valid command parameter."
        fi
else
        echo "No command parameter provided."
fi

You can see there is check if there are any parameters at all.

Then it check what parameter was typed

The test parameter has no additional parameters needed, and the say parameter requires another parameter so there is check if count of parameters is more or equal 2

All spaces in between [ and ] and other things are required so script can be interpreted correctly by sh.

 

I named my file "helper" then I made it executable by chmod +x ./helper and then you can run it like ./helper test or ./helper say Hello

You can move the file to /usr/bin so it will be globally accessible, and then you can use it without being in the same directory as the file by just helper test or helper say Hello

Link to comment
https://linustechtips.com/topic/639583-bash-or-shell-sh-linux/#findComment-8248347
Share on other sites

Link to post
Share on other sites

@Mr_KoKa so for my usage I need to make that test? since I only need one parameter behind? and the 2 last ones, the last is if you just type the file name, and the one before that is a unvalid one?

Back-end developer, electronics "hacker"

Link to comment
https://linustechtips.com/topic/639583-bash-or-shell-sh-linux/#findComment-8248403
Share on other sites

Link to post
Share on other sites

Yup, you're right. It would look like this 

if [ "$#" -ge 1 ]; then
        if [ "$1" == "cmd1" ]; then
                echo "This is cmd 1."
        elif [ "$1" == "cmd2" ]; then
                echo "This is cmd 2."
        else
                echo "$1 is not valid command parameter."
        fi
else
        echo "No command parameter provided."
fi

 

Link to comment
https://linustechtips.com/topic/639583-bash-or-shell-sh-linux/#findComment-8248423
Share on other sites

Link to post
Share on other sites

@Mr_KoKaYou don't randomly know how to get those [ INFO ] [ ERROR ] [ OK ] ? I found out how to make them manually with colors, but they are not aligned as in etc when you boot linux, and some programs etc service start, returns [ OK ] started bla bla

Back-end developer, electronics "hacker"

Link to comment
https://linustechtips.com/topic/639583-bash-or-shell-sh-linux/#findComment-8248932
Share on other sites

Link to post
Share on other sites

The only thing I can think of is to use tabualtor like

echo -e '\033[0;31m[ERROR]\033[0m\t Error message'

but without spaces in between, cause [ ERROR ] will exceed the tab.

 

You can create yourself a function.


#!/bin/bash

errorMessage() {
        echo -e "\033[0;31mERROR\033[0m $1"
}

errorMessage "Test error message"

Edit: I forgot about tab character and brackets in function example. You will figure this out. BTW, the thins you are talking about are probably some binary executables that can have their own functions to format texts and align them. (like c iomanip)

Link to comment
https://linustechtips.com/topic/639583-bash-or-shell-sh-linux/#findComment-8248986
Share on other sites

Link to post
Share on other sites

5 minutes ago, Mr_KoKa said:

The only thing I can think of is to use tabualtor like


echo -e '\033[0;31m[ERROR]\033[0m\t Error message'

but without spaces in between, cause [ ERROR ] will exceed the tab.

 

You can create yourself a function.



#!/bin/bash

errorMessage() {
        echo -e "\033[0;31mERROR\033[0m $1"
}

errorMessage "Test error message"

 

Yea thats what I did, tho I found this scrip that has this in it fn_print_warn_nl and fn_print_ok_nl and when I run those scripts and it triggers those this happens
Untitled.png

Back-end developer, electronics "hacker"

Link to comment
https://linustechtips.com/topic/639583-bash-or-shell-sh-linux/#findComment-8249015
Share on other sites

Link to post
Share on other sites

Untitled.png

This is how mine looks now, the error still looks bad the other looks just like the scripts I find does.

RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

fnerror="[ ${RED}ERROR${NC}]"
fnwarn="[ ${YELLOW}WARN${NC} ]"
fnok="[  ${GREEN}OK${NC}  ]"
fninfo="[ ${CYAN}INFO${NC} ]"

printf "$fninfo\n"
printf "$fnerror\n"
printf "$fnok\n"
printf "$fnwarn\n"

This is how I made them, how would I go by fixing the error?

Back-end developer, electronics "hacker"

Link to comment
https://linustechtips.com/topic/639583-bash-or-shell-sh-linux/#findComment-8249137
Share on other sites

Link to post
Share on other sites

4 minutes ago, Mr_KoKa said:

I don't see a really good way to fix it, it is just uneven. Maybe just add space for every of those, won't be look as bad as error without a space.

Ah, or change it to FAIL that works to. thanks anyways!

Back-end developer, electronics "hacker"

Link to comment
https://linustechtips.com/topic/639583-bash-or-shell-sh-linux/#findComment-8249167
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×