Shell tricks - Programming On Unix

Users browsing this thread: 1 Guest(s)
josuah
Long time nixers
fallbacks in pipes

On frustrating thing with busybox ps is that it fails when you add the usual `-ax' formatters to it. This might happen with other situations too.

The sh `{ ...; }` syntax permit you to have fallbacks:

Code:
{ ps -ax || ps; } | less

If `ps -ax' fails, ps is executed, and the output of either is piped into less.
Although different commands may behave differently so be cautious with this.

You may also want to check if a command exist before:

Code:
for browser in mothra netscape midori surf jumanji uzbl x-www-browser w3m links elinks lynx retawk curl
do command -v "$browser" && break
done
"$browser" "$url"

$browser will take the value of each one in the list and `command -v' will check if it exist and break the loop if so. At the end $browser will hopefully be an existing browser command.

P.S.: If you use ps in scripts, you might want to consider pgrep(1) and pkill(1), also in busybox to look for a process, rather than `ps + grep "ugly stuff"'.


Messages In This Thread
Shell tricks - by josuah - 13-11-2017, 05:27 AM
RE: Shell tricks - by josuah - 13-11-2017, 05:39 AM
RE: Shell tricks - by venam - 14-11-2017, 01:53 AM
RE: Shell tricks - by josuah - 23-11-2017, 07:59 AM
RE: Shell tricks - by josuah - 23-11-2017, 08:52 AM
RE: Shell tricks - by josuah - 04-12-2017, 07:43 AM
RE: Shell tricks - by z3bra - 04-12-2017, 08:11 PM
RE: Shell tricks - by josuah - 05-12-2017, 08:07 AM
RE: Shell tricks - by josuah - 05-12-2017, 09:02 AM
RE: Shell tricks - by josuah - 05-12-2017, 09:08 AM
RE: Shell tricks - by josuah - 08-12-2017, 12:25 PM