150
votes
How to exit a shell script if one part of it fails?
One approach would be to add set -e to the beginning of your script. That means (from help set):
-e Exit immediately if a command exits with a non-zero status.
So if any of your commands fail, ...
132
votes
Accepted
Bash throws error, line 8: $1: unbound variable
set -u will abort exactly as you describe if you reference a variable which has not been set. You are invoking your script with no arguments, so get_percent is being invoked with no arguments, ...
105
votes
Accepted
shell find -delete "directory not empty"
As @Stephen Kitt mentions, this is largely a duplicate of find -delete does not delete non-empty directories which states that you're telling it to delete directories, but the directories aren't empty ...
96
votes
Confusing use of && and || operators
Here's my cheat sheet:
"A ; B" Run A and then B, regardless of success of A
"A && B" Run B if A succeeded
"A || B" Run B if A failed
"A &" Run A in background.
93
votes
Multiple arguments in shebang
Although not exactly portable, starting with coreutils 8.30 and according to its documentation you will be able to use:
#!/usr/bin/env -S command arg1 arg2 ...
So given:
$ cat test.sh
#!/usr/bin/...
76
votes
Accepted
What's a good mnemonic for shell double vs. single quotes?
Single quotes are simple quotes, with a single standard: every character is literal.
Double quotes have a double standard: some characters are literal, others are still interpreted unless there's a ...
66
votes
Accepted
Bash multiplication and addition
Using arithmetic expansion:
for (( k = 0; k < 50; ++k )); do
a=$(( 2*k + 1 ))
echo "$a"
done
Using the antiquated expr utility:
for (( k = 0; k < 50; ++k )); do
a=$( expr 2 '*' "$k" + 1 ...
65
votes
Bash shell script output alignment
Use printf to format output (it's also more portable than echo). I would also store the real value of the colour escape sequences instead of storing them in a form that requires expansion by echo.
RED=...
63
votes
Accepted
How do I find the line number in Bash when an error occured?
Rather than use your function, I'd use this method instead:
$ cat yael.bash
#!/bin/bash
set -eE -o functrace
file1=f1
file2=f2
file3=f3
file4=f4
failure() {
local lineno=$1
local msg=$2
echo ...

slm♦
- 366k
56
votes
Bash shell script output alignment
Simply with column command:
yourscript.sh | column -t
The output:
Network 10.x.xx.xxx : Online
Network 10.x.xx.xxx : Offline
Network 10.x.xx.xxx : Offline
Network 10.x.xx.xxx ...
54
votes
Accepted
How do I replace AND (&&) in a for loop?
The equivalent to your original sequence would be:
for i in {1..20}
do
cmd $i || break
done
The difference with Amit's answer is the script won't exit, i.e. will execute potential commands that ...
54
votes
Accepted
How to run ssh command until succeeded?
Another option would be to use until.
until ssh [email protected]; do
sleep 5
done
If you do this repeatedly for a number of hosts, put it in a function in your ~/.bashrc.
repeat()
{
read -p "...
51
votes
Accepted
Replace character X with character Y in a string with bash
So many ways, here are a few:
$ string="a,b,c,d,e"
$ echo "${string//,/$'\n'}" ## Shell parameter expansion
a
b
c
d
e
$ tr ',' '\n' <<<"$string" ## With "tr"
a
b
c
d
e
$ sed 's/,/\n/g' &...
47
votes
Accepted
How to use less in a script without getting ESC escape characters?
Use -R flag:
-r or --raw-control-chars
Causes "raw" control characters to be displayed. The default is to
display control characters using the caret notation; for example, a
control-A (...
47
votes
Accepted
At sign after shebang?
This looks like a placeholder in an GNU Automake template which is going to be filled in by a configure script. So it's neither a Perl or Unix kernel thing, but a GNU autotools thing.
It is probably ...
47
votes
Accepted
Multiple arguments in shebang
There is no general solution, at least not if you need to support Linux, because the Linux kernel treats everything following the first “word” in the shebang line as a single argument.
I’m not sure ...
47
votes
Using Python in a Bash Script
To run a set of Python commands from a bash script, you must give the Python interpreter the commands to run, either from a file (Python script) that you create in the script, as in
#!/bin/bash -e
# ...
47
votes
How to run a script every 64 hours?
Just run every hour and check that the number of hours since some arbitrary instant (for instance, instant 0 of the Unix epoch time) is a multiple of 64:
0 * * * * t=$(date +\%s); [ "$(( (t / ...
45
votes
How can I suppress output only if the command succeeds?
moreutils' chronic command does just that:
chronic mycommand
will swallow mycommand's output, unless it fails, in which case the output is displayed.
45
votes
Accepted
One-liner to generate an easily memorable password?
First of all, install a dictionary of a language you're familiar with, using:
sudo apt-get install <language-package>
To see all available packages:
apt-cache search wordlist | grep ^w
Note: ...
44
votes
How to grep for same string but multiple files at the same time?
If it is simpler, you can just specify each file one after the other.
grep -E 'fatal|error|critical|failure|warning' file1.log file2.log
44
votes
Accepted
How to check if the current time is between 23:00 and 06:30
If all you need is to check if HH:MM is between 23:00 and 06:30, then don't use Unix timestamps. Just check the HH:MM values directly:
fireup()
{
while :; do
currenttime=$(date +%H:%M)
if [[...
42
votes
tmux script to launch several commands
tmux \
new-session 'compass watch /path/to/project1/compass/' \; \
split-window 'compass watch /path/to/project2/compass/' \; \
detach-client
The new-session command (which creates a new ...
41
votes
how to implement logrotate in shell script
how about using savelog?
It's available in debian and RH and pretty much every other linux distro I know of. It's a /bin/sh shell script, so should run on any other unix too.
e.g. before writing ...
41
votes
How to run ssh command until succeeded?
ssh [email protected]
until !!; do sleep 5 ; done
The !! to repeat the last command.
40
votes
Accepted
get `top` output for non-interactive shell
top -b -n 1
From man top:
-b : Batch mode operation
Starts top in 'Batch mode', which could be useful for
sending output from top to other programs or to
a file. In ...
40
votes
Accepted
reason for exec in wrapper scripts
Using exec makes the wrapper more transparent, i.e. it makes it less likely that the user or application that calls the script needs to be aware that it's a relay that in turns launches the “real” ...
40
votes
Accepted
Execute command in sftp connection through script
You can change your script to pass commands in a here-document, e.g.,
#!/bin/bash
sftp -oPort=23 [email protected]:/home/kalenpw/TestWorld/plugins <<EOF
put /home/kalenpw/.m2/repository/...
40
votes
Accepted
Linux shell script: Run a program only if it exists, ignore it if it does not exist
My interpretation would use a wrapper function named the same as the tool; in that function, execute the real tool if it exists:
figlet() {
if command -p figlet >/dev/null 2>&1
then
...
40
votes
Accepted
Does Linux support invoking a program directly via its inode number?
The short answer is no.
The longer answer is that linux user API doesn't support accessing files by any method using the inode number. The only access to the inode number is typically through the ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
scripting × 4440bash × 1521
shell-script × 1138
shell × 728
linux × 656
awk × 281
text-processing × 279
sed × 183
files × 174
command-line × 135
ssh × 134
grep × 134
cron × 102
find × 86
variable × 82
ubuntu × 75
terminal × 74
python × 74
perl × 66
rename × 65
ksh × 63
debian × 59
networking × 57
regular-expression × 52
zsh × 51