Giter VIP home page Giter VIP logo

Comments (3)

olmari avatar olmari commented on May 27, 2024

We can determine existence of an program with "which", it gives full path to program queried, or nothing is program not exist, also it gives exit status 0 when program found, and 1 if not found, so comparing exit status is propably what we want to use to determine existence of a program.

Example output:

$ which dig
/usr/bin/dig

$ printf '%d\n' $?
0

$ which ifconfig

$ printf '%d\n' $?
1

So I wrote litte test.sh to compare and tell if a program exist:

#!/bin/bash

DIG=$(which dig)
[ ${PIPESTATUS[0]} -eq 0 ] && printf "DIG exist, path: $DIG\n" || printf "DIG unavailable\n"

IFCONFIG=$(which ifconfig)
[ ${PIPESTATUS[0]} -eq 0 ] && printf "IFCONFIG exist, path: $IFCONFIG\n" || printf "IFCONFIG unavailable\n"

Which outputs correctly on mine machine:

$ ./test.sh 
DIG exist, path: /usr/bin/dig
IFCONFIG unavailable

from synth-shell.

olmari avatar olmari commented on May 27, 2024

Snippets to get an IP-number (and only the number) as result, both IPv4 and IPv6 variants:

dig:

IPv4:

dig TXT -4 +short o-o.myaddr.l.google.com @ns1.google.com | awk -F'"' '{print $2}'

IPv6:

dig TXT -6 +short o-o.myaddr.l.google.com @ns1.google.com | awk -F'"' '{print $2}'

nslookup:

IPv4:

nslookup -q=txt o-o.myaddr.l.google.com 216.239.32.10 | awk -F \" 'BEGIN{RS="\r\n"}{print $2}END{RS="\r\n"}'

IPv6:

nslookup -q=txt o-o.myaddr.l.google.com 2001:4860:4802:32::a | awk -F \" 'BEGIN{RS="\r\n"}{print $2}END{RS="\r\n"}'

curl:

IPv4:

curl -s https://api.ipify.org

IPv6:

curl -s https://api6.ipify.org

wget:

IPv4:

wget -q -O - https://api.ipify.org

IPv6:

wget -q -O - https://api6.ipify.org

from synth-shell.

olmari avatar olmari commented on May 27, 2024

Implemented this in 40f9261

It check available programs with whichand then uses first found program and only tries next program if result is empty. Optimally we'd use exit result but so far we rely on result having anything in it and supressing any runtime errors from saving to result.

from synth-shell.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.