Skip to main content
12 votes

What does a minus sign inside of dollar brackets of a shell script mean?

It should be emphasized that with ${parameter-word} it means an unset variable ("parameter"). But not an empty one (null). While with ${parameter:-word}, word value is used when parameter is ...
papo's user avatar
  • 239
11 votes

Echo each shell script command with a timestamp

In Bash, you can use PS4 - which is used by set -x to prefix tracing output. PS4='+ $(date "+%s.%N")\011 ' set -x ... \011 is the horizontal tab character
ArtBIT's user avatar
  • 211
11 votes

Robust way to resolve a DNS address in a script (IPv4 (A) and IPv6 (AAAA))?

I'm unsure if you want to actually query nameservers or rather rely on your system's name resolution: $ getent ahosts one.one.one.one. | awk '{ print $1 }' | sort -u 1.0.0.1 1.1.1.1 2606:4700:4700::...
fuero's user avatar
  • 9,899
8 votes
Accepted

Echo each shell script command with a timestamp

in Bash, execution of code after each command can be achieved using the "trap" builtin and the "DEBUG" level. in bash do: help trap or refer to bash manual page, and look for the trap section ...
laurent's user avatar
  • 126
8 votes

Loop through servers and run command

There is solution with -n -x parameters: http_status=$(ssh -nx name ps -ef | grep -v grep | grep $service | wc -l) The -x disables X11 forwarding to get rid of possible X11 forwarding request failed ...
Jaroslav Kucera's user avatar
8 votes
Accepted

Closing All Shared Files on the Network

The command you are using is not a native PowerShell command. However, PowerShell does come with a lot of functions to control SMB connections and shares. What you are looking for could be this: Get-...
Tobias's user avatar
  • 1,244
8 votes
Accepted

How to start a jar file using systemd

Using systemd directives in place of shell results in a more consistent environment. This includes not relying on $PATH or the working directory by providing full paths where they are known. Neither &...
John Mahowald's user avatar
8 votes

Robust way to resolve a DNS address in a script (IPv4 (A) and IPv6 (AAAA))?

You are on the right track with dig. You can suppress that particular cookie related error (per the man page). Below, I am also choosing to redirect all errors to /dev/null just in case, as you wanted ...
JG7's user avatar
  • 206
8 votes

Robust way to resolve a DNS address in a script (IPv4 (A) and IPv6 (AAAA))?

getent ahosts[v4|v6] and generally anything that uses getaddrinfo() is the right answer, as it will use the same name resolution mechanisms (DNS, mDNS, NetBIOS, NIS, LDAP…) as the rest of the system, ...
grawity's user avatar
  • 18.8k
7 votes
Accepted

Loop through servers and run command

You need to redirect the stdin of SSH command in your loop to nowhere to prevent reading all your lines. This should do it: http_status=$(ssh $name "ps -ef | grep -v grep | grep $service | wc -l" <...
Khaled's user avatar
  • 38k
7 votes
Accepted

Using blkid to check if an attached EBS volume is formatted

You can check the details of a specific device and act based on that: blkid --match-token TYPE=ext4 /dev/nvme123 || mkfs.ext4 -m0 /dev/nvme123 What this does is: blkid checks whether /dev/nvme123 is ...
MLu's user avatar
  • 26.3k
7 votes
Accepted

Can't access instance after setting up iptables rules

For rules in the OUTPUT chain you should specify the source port match (--sport), not the destination port (--dport). Anyway DROP policy in the OUTPUT chain isn't a common practice. Read the iptables ...
Anton Danilov's user avatar
6 votes

Trying to execute this shell script but getting this error

Your script has DOS line endings. Convert them to Linux line endings either in your editor or with tools like dos2unix, recode etc. DOS/Windows usually ends lines with CR+LF, while Linux only uses ...
Sven's user avatar
  • 101k
6 votes
Accepted

Running a binary as a service in RedHat/CentOS

Have you looked into the below error message? Apr 16 06:29:32 matrix-pipeline-b-01 executePipeline.sh[10195]: sudo: no tty present and no askpass program specified Apr 16 06:29:32 matrix-pipeline-b-...
Timothy Pulliam's user avatar
6 votes
Accepted

Is it possible to listen to a TCP port only with a shell, with no additional tools?

No, a basic Bourne/POSIX shell (/bin/sh) cannot be expected to include any built-in facilities for TCP connections. See comparision of command shells in Wikipedia. The bash shell would have TCP and ...
telcoM's user avatar
  • 4,896
5 votes
Accepted

Can't get cron to output a GUI message / how to make user aware cron is being executed

You can use the notify-send application to send a notification to the logged in user's desktop. This program is in the libnotify-bin package. For example: notify-send --icon install "Cron job&...
Michael Hampton's user avatar
4 votes

Get list of transferred files from rsync?

Method 1 You can use --info=NAME option(only versions later than rsync 3.1.0 are supported) rsync -azPrui --info=NAME /path/from/source/ /path/to/destination/ use rsync --info=help to get the value ...
Willis's user avatar
  • 141
4 votes

Automated graceful reload of gunicorn in production

More detailed explanation for editing your gunicorn service file (based on the answer of @jordanm): Edit your service file /etc/systemd/system/my_task.service or a similar path [Unit] Description=My ...
Benjamin Crouzier's user avatar
4 votes
Accepted

Parallelize curl requests

Xargs can do that in foreground and it gives you control over parallelism and batching. Curl will reuse an open connection to send all requests in a batch. -P 42 defines how many curls to run at a ...
darw's user avatar
  • 178
4 votes
Accepted

How can we limit and give space in after each special character in bash script output?

You can format your rpm output using --qf. Query formats are modified versions of the standard printf(3) formatting. The format is made up of static strings (which may include standard C character ...
kenlukas's user avatar
  • 3,424
4 votes

How to delete docker images older than x days from docker hub using a shell script

Below script will delete all images in all repositories of your docker hub account which are older than 50 days. #!/bin/bash #Script will delete all images in all repositories of your docker hub ...
amit's user avatar
  • 131
3 votes
Accepted

getting multiple issues while creating a scrip to update hostnames in /etc/hosts file?

I have not tried reading CSV in shell, so this is going to be an incomplete reply. I usually use Perl or AWK to do such things. The first problem is that shell is using blank to delimit the ...
Chris Maxwell's user avatar
3 votes

Connect to MySQL through command line without using root password?

Similar question exists on StackOverflow. To summarize my answer from there. You can export MYSQL_PWD=yourverysecretpassword. The upside of this method over using a configuration file is that you ...
chutz's user avatar
  • 8,338
3 votes

Is it possible to set a timeout on openssl's s_client command?

for the first loop: while read servername;do timeout 2 bash -c "/dev/tcp/$servername/$Port" && echo Port open. || echo Port closed. done But the open ports is more dificult: timeout 1 ...
erik's user avatar
  • 31
3 votes

Delete a zone from named.conf with shell script

Regarding the actual problem you are solving, I think it'd probably be worth looking into using rndc addzone/rndc delzone (reliant on the allow-new-zones option) to allow automation of adding/deleting ...
Håkan Lindqvist's user avatar
3 votes
Accepted

Jenkins - environment variable not setting up from pipeline

Replace your double quotes with single quotes to prevent $IPADDRESS from being interpreted as a Groovy variable: sh ''' IPADDRESS = \$(docker inspect -f "{{ .NetworkSettings.IPAddress }}" ...
jayhendren's user avatar
  • 1,074
3 votes
Accepted

How to replace some character from a string in UNIX?

From the "test, [ -- condition evaluation utility" manpage (accessible using the command man [ or man test), the '-ne' option is used for comparing integers. Use the '=' operator for strings....
Chris Simons's user avatar
3 votes
Accepted

convert date time epoch in miliseconds to UTC human readable format

Would: date -d @$((1549950859468 / 1000)) -u do?
Tomek's user avatar
  • 3,811
3 votes

Shell script prevent switching to other shell or escape from it

If you actually look at this script you will see that, as the last thing it does, it calls: exec zsh -l Just before that, you will see that it checks to see if the RUNZSH variable is set to no. ...
Michael Hampton's user avatar
3 votes
Accepted

Run a multiline bash script piped from stdin

If you quote or escape the here-document delimiter (i.e. <<'EOF' or <<\EOFinstead of just <<EOF), the shell won't do expansions (of variables etc) on the content. cat <<'EOF' |...
Gordon Davisson's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible