6 votes
Accepted

Prevent shell script from hanging on a line

Instead of piping df's output into grep (which causes df to query all mounted filesystems, even those which are not responding - e.g. a hung NFS mount), give df the fileystem(s) you want info on as ...
user avatar
  • 66.4k
5 votes

How to ignore all text containing special character more than once?

$ grep -v '_.*_' file apple_ig dog_ig orange_ig goat_ig
user avatar
  • 21.8k
3 votes
Accepted

How to execute the next command in pipe only if previous have at least one result?

Since the N-ary stop operation of Docker is silly with regard to zero containers, what you can do is treat docker stop container as as a unary operation which requires exactly one argument, and do ...
user avatar
  • 6,631
3 votes

How to display a prompt and prevent execution against basic but dangerous commands like mv or rm

I'd use a wrapper function: you can add this to your .bashrc (untested) mv () { case $1 in -*i*) # ok, used `mv -i ...`: invoke the mv command, passing all args command mv &...
user avatar
3 votes
Accepted

How to ignore all text containing special character more than once?

With awk, to print only lines having maximum one underscore. $ awk -F_ 'NF<=2' file apple_ig dog_ig orange_ig goat_ig
user avatar
  • 5,803
2 votes
Accepted

Confusing behavior of stdout/stderr and /dev/null

echo "Hello World" 1>&2 1>&1 redirects file descriptor 1 (standard out) to whatever file descriptor 2 (standard error) is pointing at, and then redirects file descriptor 1 to ...
user avatar
2 votes
Accepted

How to make libreoffice headless conversion act differently on file loading error

In an ideal world libreoffice will let you know with an exit code that the conversion did not take place. But it seems that's not the case. So you have at least two alternatives, one is parsing the ...
user avatar
1 vote

What's a clean way to run a specific command C for each line L of a given file F and then move every L where C(L) ran unsuccesfully?

Using bash, you can loop for the urls and test the curl command, --fail option of curl seems to be good for use inside scripts, see: How to check whether a command such as curl completed without error ...
user avatar
  • 5,803
1 vote
Accepted

extract tar then enter its directory (shell scripting)

A shell script can never change the working directory for its parent process (the shell invoking the script). So although you may change to the created directory in the script, as soon as the script ...
user avatar
  • 267k
1 vote

How to display a prompt and prevent execution against basic but dangerous commands like mv or rm

While @glennJackman's approach with a wrapper that checks if the first arguments starts with - and contains i is likely good enough in most common cases, there are a few cases where it fails: It gets ...
user avatar
1 vote

How to execute the next command in pipe only if previous have at least one result?

You can silence shell output using redirects, see this post for more details: https://stackoverflow.com/questions/2292847/how-to-silence-output-in-a-bash-script I might use something more like this to ...
user avatar
  • 107
1 vote

How to display a prompt and prevent execution against basic but dangerous commands like mv or rm

The easiest way is to create an alias. alias mv="mv -i" More complicated, but more customizable, would be to create an mv shell script, put it into the /bin, and the real mv move to /sbin ...
user avatar
  • 1,105
1 vote
Accepted

Periodically transfer files from two remote servers to single host using shell script

Use an array to contain the pair of hosts. Initially the array can contain just a single element, and you can add the second host later. Here I've included two sets of credentials so you can see the ...
user avatar
  • 88.6k
1 vote
Accepted

Extract field and number of occurrences per line

Try this: $ awk -v FS="', '" '{print $4 " |"NF-1 " " NR}' file If you have blank lines in file, you may want to use: $ awk -v FS="', '" 'NF >= 1 {print $4 &...
user avatar
  • 2,229
1 vote

Prevent sourcing a bash script, in the script itself

Based on @roaima ' s answer: add this line early in your script. return 0 2>/dev/null || : It works even after set -e It will not show an error message when not using source. It prevents running ...
user avatar
  • 131

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