50
votes
Accepted
What is <<!END in a Bash script?
It's a Here Document -- the lines between <<!END and !END are fed to the stdin of wlst.sh
It could also be expressed as:
echo "connect('user','pw');
p=redeploy('application');
p.printStatus();...
48
votes
Accepted
Is it safe to open a file that is being written by a running script?
Reading the file is safe, although double clicking you mentioned will probably open the file in some editor that will offer you an option to make changes and save them. Missclicks happen, so I ...
21
votes
Accepted
Dynamically append text to filenames in Bash
These solutions you link to are in fact quite good. Some answers may lack explanation, so let's sort it out, add some more maybe.
This line of yours
for file in *.txt
indicates the extension is known ...
21
votes
Accepted
Get bash to respect quotes when word splitting subshell output
Bash really doesn't have a good way to parse a string into substrings, while respecting quotes. Whether it's coming from a command expansion (that is, $( ) -- what I think you're calling a subshell) ...
20
votes
Accepted
When pressed ctrl+c only break out of current function and not whole bash script
Analysis
When you hit Ctrl+c, the line discipline of your terminal sends SIGINT to processes in the foreground process group.
Bash, when job control is disabled, runs everything in the same process ...
17
votes
Edit xml file using shell script / command
xmlstarlet edit --update "/book/fiction[@type='b']/author/@type" --value "Local" book.xml
15
votes
Insert quote into string variable in bash
In bash you can use \ as an escape character what whatever follows it. In your case, use it like this:
string="\"desktop/first folder\""
15
votes
Is it safe to open a file that is being written by a running script?
As long as you are not writing to it, it should be okay.
However, I would recommend using
tail -f log_file
in another terminal.
This command will "follow" the file log_file and write the newly ...
15
votes
Accepted
What is awk '{ print $2; }' doing exactly?
ps aux lists all processes.
grep 'sidekiq 5' only displays those lines from the list that contain the string "sidekiq 5".
grep -v grep removes those lines that contain the string "grep&...
14
votes
How to compare numeric strings in Bash?
No need for Python, awk or bc etc. Bash can strip off the minor part of the version number:
$ cat t.sh
#! /bin/bash
DEBVERS=12.11
echo "DEBVERS = " $DEBVERS
major=${DEBVERS%.*}
echo "...
11
votes
how to see git log automatic refresh
You can make that a one-liner with watch:
watch --color -n 3 git log --all --decorate --oneline --graph --color=always
Tweak the -n flag to change the refresh rate.
11
votes
Accepted
Add a new element to existing JSON array
Do not try to parse JSON text with standard text processing tools like awk, sed or without JSON modules in perl, as they are non JSON syntax aware. jq is a lightweight JSON processor that allows you ...
10
votes
how to gzip and scp at the same time
In case you need to get the files/directories from a remote server, into a local archive, you can use tar + gzip inside ssh, and redirect to a local file. For example:
ssh user@server "sudo tar cvzf -...
9
votes
Accepted
Insert quote into string variable in bash
If you don't do variable substitution, using single quotes as delimiters, so you can use double quotes in your string:
string='"desktop/first folder"'
9
votes
Remove duplicate files by comparing them with MD5 RECURSIVELY
I'd recommend something like the following instead:
find . -type f \
| xargs md5sum \
| sort -k1,1 \
| uniq -Dw32
This will list all duplicated files in groups of files that have an ...
9
votes
Accepted
Test condition in if command
You may know it or not, but let's make it clear: [ is not a part of the if syntax. I mean if [ … behaves like if true, if false or if any_command, where [, true, false and any_command are commands. ...
9
votes
Bash script to toggle monitor input from HDMI to DisplayPort and vice-versa
I am the developer of ddcutil
How feature x60 (Input Source) behaves varies by monitor. Some monitors accept commands only from the current input source, others accept input from any source. Given ...
9
votes
How do I run a sudo command on a remote machine using ssh?
Changing the behavior of ssh
When you run ssh without a command and there is a local pseudo-terminal, the tool allocates a pseudo-terminal on the remote side automatically. Usually you access an ...
9
votes
Accepted
Ffmpeg: suppress warning when writing to a single image
Either
ffmpeg -i "src.png" -vf "$vf" -vframes 1 "dst.png"
or
ffmpeg -i "src.png" -vf "$vf" -update true "dst.png"
The former is preferable,...
9
votes
Accepted
Bash [[ test =~ regex ]] vs perl command result
There are several different types of Regular Expression, each one adding more operators (and therefore requiring more characters to be escaped if they are to be considered literals).
The =~ operator ...
8
votes
Which bash rc files are run on non-interactive/non-login shells?
The answer to your specific question is that often only /etc/bash.bashrc (or /etc/bashrc) is loaded.
There seem to be a couple of ways to address this, most are workarounds unfortunately. In no ...
8
votes
Accepted
Cannot scp Over a Shared Connection
ssh and scp use the -S option for different purposes.
ssh:
-S ctl_path
Specifies the location of a control socket for connection sharing, or the string “none” to disable connection sharing. Refer ...
7
votes
Is there a native alternative to the `watch` command; for Darwin/OS X
Based on @Spiff awesome answer, I improved it a bit to avoid flickering/flashing on each execution:
fakewatch () { while true; do DATE=$(date); RESULT=$(${@}); clear; echo "$DATE"; echo "$RESULT"; ...
7
votes
What commands would I use to create two child processes serially?
I'm not sure there is an elegant command / answer to the question you asked (i.e. reach a certain point in its execution) - but there are tools / techniques which can be used to bodge up a solution, ...
7
votes
Accepted
What commands would I use to create two child processes serially?
You can use the xargs utility for this. It can run a command for each line on stdin, so we just need to ensure that xargs gets as input a single line on stdin at the time it should start the command, ...
7
votes
Accepted
sh syntax to handle zero files matching a wildcard, as well as more?
The simplest portable way to do this is to skip the loop for if the expansion doesn't produce something that actually exists:
for f in *.ext1 *.ext2; do
[ -e "$f" ] || continue
handle "$f"
done
...
7
votes
Accepted
How can I get the result of a command and its return code at the same time in a Bash script?
The problem here is that in the pipeline cat "$filename" | wc -l, when the file doesn't exist cat will exit with an error, but wc -l will successfully count the 0 lines of text it receives from cat. ...
7
votes
Accepted
How to unzip a file with a hyphen at the beginning of the file name
Try this method of prepending - stuff.zip with ./:
unzip -l './- stuff.zip'
Or use this method that does’t need quotes — single or double — but escapes the space after the hyphen:
unzip -l ./-\ stuff....
6
votes
Accepted
how to see git log automatic refresh
Based on this answer from SO, you can stop the need to quit by adding --no-pager immediately after the git.
It's not the log command that is waiting for q, it's the less tool that is doing the ...
6
votes
What commands would I use to create two child processes serially?
This is just an idea, but have the first process pipe its output to grep. Have grep pipe its output to a read line loop, and compare each line with what you're looking for. Once found, execute proc2.
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
bash-scripting × 974bash × 571
linux × 339
shell-script × 162
shell × 94
command-line × 90
script × 60
ubuntu × 40
macos × 36
sed × 32
unix × 31
terminal × 24
awk × 23
ssh × 22
ffmpeg × 19
windows × 16
regex × 15
cygwin × 15
grep × 15
find × 14
cron × 14
bashrc × 14
batch × 12
python × 12
environment-variables × 12