You are not logged in.

#1 2015-12-07 18:13:47

damo
....moderator....
Registered: 2015-08-20
Posts: 5,157

Script snippet to test if required tools exist

@johnraff gave me this, which can be incorporated in any script. This example comes from a script which has calls for `xwininfo`, `xprop` and `yad`:

## check necessary tools are installed
required_commands=(xwininfo xprop yad) # array, space-separated words

error_exit() {
    echo "$0 error: $1" >&2
    exit 1
}

missing_commands=()
for i in "${required_commands[@]}"
do
    hash $i || missing_commands+=(" $i")
done
[[ ${missing_commands[0]} ]] && error_exit "This script requires the following commands: ${missing_commands[*]}
Please install the packages containing the missing commands
and rerun the script."

Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#2 2015-12-07 19:31:04

Horizon_Brave
Operating System: Linux-Nettrix
Registered: 2015-10-18
Posts: 1,473

Re: Script snippet to test if required tools exist

Neat! The only thing that threw me off, is what with the [@]  in "${required_commands[@]}"
What does the @ do?  Does it serve as a sort of place holder for the array?

Also is it possible to create an array that's separated by other character, that are not spaces?


"I have not failed, I have found 10,000 ways that will not work" -Edison

Offline

#3 2015-12-07 19:42:32

porkpiehat
Member
Registered: 2015-10-27
Posts: 31

Re: Script snippet to test if required tools exist

^ http://www.linuxjournal.com/content/bash-arrays

"${arr[@]}" returns each item as a separate word.

Offline

#4 2015-12-07 19:46:30

damo
....moderator....
Registered: 2015-08-20
Posts: 5,157

Re: Script snippet to test if required tools exist

Bash wiki: Arrays

"@" expands to all individual array elements


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#5 2015-12-07 20:47:56

Snap
Member
Registered: 2015-10-02
Posts: 465

Re: Script snippet to test if required tools exist

Into the stash. Thanks a bunch.

Offline

#6 2015-12-07 21:26:45

Horizon_Brave
Operating System: Linux-Nettrix
Registered: 2015-10-18
Posts: 1,473

Re: Script snippet to test if required tools exist

porkpiehat wrote:

^ http://www.linuxjournal.com/content/bash-arrays

"${arr[@]}" returns each item as a separate word.

damo wrote:

Bash wiki: Arrays

"@" expands to all individual array elements

Thanks guys. I'm still new in my Bash Scripting life, and haven't gotten around to working with arrays yet.

So this script could have also been written by assigning the tools individually to variables, and testing them one by one, but this is obviously more effecient...


"I have not failed, I have found 10,000 ways that will not work" -Edison

Offline

#7 2015-12-08 03:19:21

johnraff
nullglob
From: Nagoya, Japan
Registered: 2015-09-09
Posts: 5,957
Website

Re: Script snippet to test if required tools exist

Also note the * instead of @ where the array members are put inside a single string.

string="Some stuff: ${missing_commands[*]} more stuff."

If you use @ your string will get broken up at each array element.

"Some stuff: <first element>" "<next element>" "<last element> more stuff."

I made that mistake a while ago, and it can cause subtle errors which are hard to find.


John
--------------------
( a boring Japan blog , Japan Links, idle twitterings  and GitStuff )
In case you forget, the rules.

Online

#8 2015-12-08 06:21:59

tknomanzr
BL Die Hard
From: Around the Bend
Registered: 2015-09-29
Posts: 1,029

Re: Script snippet to test if required tools exist

This looks useful and thanks for the tip regarding the difference between @ and *. The first you would use when you need to build an array up, I would presume and the latter would be used when you need to build a single string out of an array.

Offline

#9 2015-12-08 14:28:11

Horizon_Brave
Operating System: Linux-Nettrix
Registered: 2015-10-18
Posts: 1,473

Re: Script snippet to test if required tools exist

johnraff wrote:

Also note the * instead of @ where the array members are put inside a single string.

string="Some stuff: ${missing_commands[*]} more stuff."

If you use @ your string will get broken up at each array element.

"Some stuff: <first element>" "<next element>" "<last element> more stuff."

I made that mistake a while ago, and it can cause subtle errors which are hard to find.


See...those type of nuiances, would screw me up and send me into a hulk rage moment. Is it then just better to use the *  instead for all purposes, instead of switching back and forth from @ to *?


"I have not failed, I have found 10,000 ways that will not work" -Edison

Offline

#10 2015-12-08 15:07:58

damo
....moderator....
Registered: 2015-08-20
Posts: 5,157

Re: Script snippet to test if required tools exist

Horizon_Brave wrote:

.... Is it then just better to use the *  instead for all purposes, instead of switching back and forth from @ to *?

You should read my link tongue

For clarification: When you use the subscripts @ or * for mass-expanding, then the behaviour is exactly what it is for $@ and $* when mass-expanding the positional parameters. You should read this article to understand what's going on.


Be Excellent to Each Other...

FORUM RULES and posting guidelines «» Help page for forum post formatting
Artwork on DeviantArt  «» BunsenLabs on DeviantArt

Offline

#11 2015-12-10 01:34:47

Horizon_Brave
Operating System: Linux-Nettrix
Registered: 2015-10-18
Posts: 1,473

Re: Script snippet to test if required tools exist

damo wrote:
Horizon_Brave wrote:

.... Is it then just better to use the *  instead for all purposes, instead of switching back and forth from @ to *?

You should read my link tongue

For clarification: When you use the subscripts @ or * for mass-expanding, then the behaviour is exactly what it is for $@ and $* when mass-expanding the positional parameters. You should read this article to understand what's going on.


Ugh...Much to learn, I have...


"I have not failed, I have found 10,000 ways that will not work" -Edison

Offline

Board footer

Powered by FluxBB