New answers tagged scripting
13
votes
Accepted
Strange variable scope behavior when calling function recursivly
Do I get it right that you want to count all files in a directory tree, or something like that? I.e. you want the variable count to be global?
Your issue is here:
find $1 | while read line; do
See ...
2
votes
Bash script that takes multiple path arguments and checks if files can be successfully created there
Many Bourne-derived shells have primitives for checking this kind of thing, making touch unnecessary under shells which have a test-like utility with -d and -w operators:
$ mkdir dir{1..5} dir\ \"...
3
votes
Bash script that takes multiple path arguments and checks if files can be successfully created there
JSON is a poor choice of format as, when UTF-8 encoded (as it's often required to these days, and many tools don't support non-UTF-8 encoded JSON), its strings can't contain arbitrary file paths (...
1
vote
Bash script that takes multiple path arguments and checks if files can be successfully created there
Since UNIX paths may very well contain double-quotes and other characters that when interpolated directly would break a JSON file, it would be safer to let a JSON processor handle the task.
Also the ...
0
votes
Bash script that takes multiple path arguments and checks if files can be successfully created there
Instead of touch, I would test to see if the path is writeable [[ -w "$p" ]] .. To specifically test for directory and writable, then [[ -d "$p" && -w "$p" ]]. ...
1
vote
Bash script that takes multiple path arguments and checks if files can be successfully created there
Using associative array from bash4+.
#!/usr/bin/env bash
declare -A paths ##: declare associative array
for f; do ##: Loop through all the give arguments
[[ -z "$f" ]] && ...
2
votes
Accepted
Bash script that takes multiple path arguments and checks if files can be successfully created there
Your script and the df script test different things. One tests "can I write to this directory" the other could test "is there space on this disk".
Assuming you wanted "can I ...
0
votes
Run a command if stdin is empty
If the point is to pass the output of the command along to another command, but produce some alternative output if that output is empty, then you can do:
cmd |
perl -pe 'END{print "alternative ...
1
vote
Run a command if stdin is empty
grep can tell you if it has found a match via its exit status, there's no need to check its output (which we can suppress with -q¹):
if
! ip address |
grep -F 'inet ' |
grep -qvFe ' tun' -e '...
-1
votes
Run a command if stdin is empty
I needed to execute a command if and only if there are no tracked-but-modified files, no staged files, and no untracked files (I looked at the answers to this question for this last condition; that is,...
2
votes
Accepted
bash script to run a python command with arguments in batch
Maybe it would make more sense to write it as:
#! /bin/zsh -
topdir=/Users/mvalvano/DeepSecE
ret=0
for filename in $topdir/myruns/*.faa(N); do
outdir=$filename:r
mkdir -p -- $outdir &&
...
5
votes
bash script to run a python command with arguments in batch
Replace test with "${filename%.faa}" to get the name of the file with .faa removed. You should also quote "${filename}" to avoid problems in case of filenames with spaces.
#!/bin/...
0
votes
remove title bar of another program
I can confirm the devilspie2 solution by @scrat.squirrel does work in XFCE4 (Void Linux, Xorg), unlike the xprop above or Python scripts available elsewhere (which seem to require Python2, now EOL).
...
1
vote
How to copy the value from one "column" to another in Bash?
Here is the solution using any awk:
awk -F\" 'BEGIN{OFS=FS} {split($4,a,"@");$2=a[1]}1' <input file> > <output file>
3
votes
How to copy the value from one "column" to another in Bash?
A single sed should sort this out. Pick out the email address and substitute it back into the username part:
echo 'username="user1" email="[email protected]"' |
sed -E 's/(...
1
vote
Preview locales
You could do something like:
LC_ALL=en_DK.UTF-8 locale -kc $(
locale | sed '/^LC_/!d;/^LC_ALL=/d;s/=.*//'
)
(assuming $IFS not modified from its default) which would give you keyword=values for ...
0
votes
Preview locales
You can run the following code. Add printf's for any value you want to display.
#!/usr/bin/env bash
# check all locales you currently have generated on your machine
# and display the numeric ...
2
votes
Accepted
How does "Useless cat. ... or 'cmd file | ..' instead." should be applied?
In this case cmd is referring to grep itself. grep can read from a file so your full command could (and arguably should) be simply this:
grep -c '^ID=ubuntu$' /etc/os-release
The reason it suggests &...
0
votes
Why does fdisk sometimes ask what kind of partition to make and sometimes not?
fdisk seems to change behavior based on the partition table type. For example, if you use parted to set "mklabel gpt" instead of "mklabel msdos" you won't get the primary vs ...
0
votes
How to run a command that requires input/output location on all folders in a directory, while correspondingly naming the new output folders?
This is my solution.
#!/bin/bash
BASEDIR="/media/lab/DATA/Avery"
INDIR="$BASEDIR/raw_pod5"
OUTDIR="$BASEDIR/dorado7_basecalling"
DORADO="/path/to/dorado_command&...
Top 50 recent answers are included
Related Tags
scripting × 4504bash × 1547
shell-script × 1161
shell × 734
linux × 664
text-processing × 287
awk × 284
sed × 185
files × 177
command-line × 136
ssh × 135
grep × 135
cron × 104
find × 88
variable × 82
ubuntu × 75
terminal × 73
python × 73
rename × 68
perl × 66
ksh × 63
debian × 61
networking × 56
zsh × 54
regular-expression × 53