This question already has answers here: How to check if running as root in a bash script (21 answers) Closed 23 days ago. How can I check whether a user is root or not within a BASH script? I know I can use or but if the script was invoked via fakeroot, UID and EUID are both 0 (of course,
Tag: bash
Programatically activate and deactivate network interfaces using nmcli
I wrote a script that deactivates all the interfaces available using nmcli commands such as: I would now like to reactivate these interfaces; there is no nmcli dev connect iface … command, and I’m stuck. In particular, my problem is to reactivate the Ethernet interface. Thanks in advance for your help! …
core dumped message is not captured in STDERR
I used to call my program with this line in bash shell to capture all stdout and stderr messages in the log file The log file shows no error but by examining the log, it’s obvious that there’s a problem and program terminates abruptly in the middle of execution. I have also tried these but the res…
sed/awk – remove a multiline block if it contains multiple patterns
In an xml file a multiline block is identified by <start></start>. I need to find and delete these multiline blocks if they contain a set of pattern in any order (pattern1, pattern2, etc). For example, in the following: if searching for pattern1 only, blocks 1, 3, 4 should be deleted if searching …
How can I *only* get the number of bytes available on a disk in bash?
df does a great job for an overview. But what if I want to set a variable in a shell script to the number of bytes available on a disk? Example: But I just want to return 33333333 (bytes available on /), not the whole df output. Answer Portably: The -P option ensures that df will print output in the
Get Subnet mask in Linux using bash
I am using bash to get the IP address of my machine with that script: And now I am trying to get the Subnet Mask in this type: But I have no idea how can I do that. Answer there are couple of ways to achieve this: first: to print the mask in format 255.255.255.0, you can use this: second:
Circular SSH Tunnel
Is is possible to create a circular SSH Tunnel? local1 >SSH_TUNNEL> remote1 >SSH_TUNNEL> local1 On “Remote1” I create a TAR that I want to pipe to STDOUT ( – ) … can I then TAR – . | >SSH_TUNNEL> local1 ssh user@remote1 “tar -cvf – . | ssh user@local…
Combining two bash commands
If found this code which gives pi Ip address and this which sends 1.2.3.4 off to dweet.io stream How can I get the output from 1st to replace the 1.2.3.4 in second please? Answer Save the output of the first command in a variable: Btw, if your raspberrypi is running raspbian, then a much cleaner way to get th…
Script to generate MD5 hash with openssl
I’m trying to generate some MD5 hashes with openssl for use with chpasswd Ex. CSV file: Script I created: However, if I take any MD5 generated from this script and try to use it with chpasswd it does not work. This password will fail If I try to do this without the script by hand it works: Answer Your C…
Using a shell commands in c++
Here’s a terminal command: Here’s my code: The above code, when run, returns this message: How can I place the ‘/’ symbol, for this command to work correctly? Answer Your problem is not in C++. You are invoking your command with popen, and popen runs your command in sh shell, that does…