Replace command output with a custom message

Asked

Viewed 102 times

1

I am creating a script where at the end of it I run a command that displays a very large output of information. Is it possible to override this output? That is, instead of showing the default output show only one ex message: Connected. ?

The command to execute would be:

sudo openvpn --config srvproxy-udp-1194-config.ovpn

His exit is usually like this:

Thu Sep 21 09:48:48 2017 WARNING: file 'srvproxy-udp-1194-tls.key' is group or others accessible Thu Sep 21 09:48:48 2017 Openvpn 2.4.0 x86_64-pc-linux-gnu [SSL (Openssl)] [LZO] [LZ4] [EPOLL] [PKCS11] [MH/PKTINFO] [AEAD] built on Jun 22 2017 Thu Sep 21 09:48:48 2017 library versions: Openssl 1.0.2g 1 Mar 2016, LZO 2.08 Enter Auth Username: xxxxxx Enter Auth Password: ******* Thu Sep 21 09:48:54 2017 TCP/UDP: Preserving recently used remote address: [AF_INET]200.210.150.105:1194 Thu Sep 21 09:48:54 2017 UDP local link (bound): [AF_INET][undef]:0 Thu Sep 21 09:48:54 2017 UDP link remote: [AF_INET]200.210.150.105:1194 Thu Sep 21 09:48:54 2017 WARNING: this Configuration may cache passwords in memory -- use the auth-nocache option to Prevent this Thu Sep 21 09:48:54 2017 [www.xxxxxxx.com.br] Peer Connection Initiated with [AF_INET]200.210.150.105:1194 Thu Sep 21 09:48:55 2017 TUN/TAP device tun0 opened Thu Sep 21 09:48:55 2017 do_ifconfig, tt->did_ifconfig_ipv6_setup=0 Thu Sep 21 09:48:55 2017 /sbin/ip link set dev tun0 up mtu 1500 Thu Sep 21 09:48:55 2017 /sbin/ip addr add dev tun0 local 172.8.0.6 peer 172.8.0.5 Thu Sep 21 09:48:55 2017 Initialization Sequence Completed

  • Which command is executed?

  • @Zumodevidrio edited the question

  • How do you know if it’s finally connected or not?

3 answers

2

Have you tried:

sudo openvpn --config srvproxy-udp-1194-config.ovpn > /dev/null && echo "Conectado" || echo "Ops, tem algo errado"

?

  • What I don’t like about the short circuit is the possibility of the command after the && go wrong, then he would go to the command of || (do not know case of failure of echo, but I have neurosis with it); but I would do so anyway. Just note that it may be necessary to play the error output to /dev/null also

  • 1

    In this case adding an Exit 0 in the command after && might work, example: service mysql status > /dev/null && (echo "UP"; Exit 0) || (c=$? ; echo "DOWN"; (Exit $c))

  • 1

    Could use the true also, the command guaranteed to return successfully

1

You can use an if in your script by discarding the output of the command:

#!/bin/bash

if openvpn --config srvproxy-udp-1194-config.ovpn >/dev/null 2>&1 ; then
        echo "Conectado"
else
        echo "Não conectado"
fi

If you need to save the output, you can redirect it to a log:
if openvpn --config srvproxy-udp-1194-config.ovpn > /tmp/openvpn.log ; then

  • The way is around, I’m just trying to adjust to run this if calling the command.

  • @Douglas Which part didn’t work? The snippet I sent uses the same command as the original post.

0

You can do it this way:

Let’s use the ls command as an example

ls > saida.txt

it will throw the entire output into the output.txt file and will not show anything on the screen, you can analyze the result in the output.txt file and then send the message to the user.

  • There would be no simpler way ?

  • It depends, what do you want to do? What command do you want to execute?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.