Skip to main content

New answers tagged

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 ...
ilkkachu's user avatar
  • 142k
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\ \"...
Jim L.'s user avatar
  • 8,322
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 (...
Stéphane Chazelas's user avatar
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 ...
kos's user avatar
  • 3,640
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" ]]. ...
rr0ss0rr's user avatar
  • 362
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" ]] && ...
Jetchisel's user avatar
  • 1,272
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 ...
Stephen Harris's user avatar
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 ...
Stéphane Chazelas's user avatar
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 '...
Stéphane Chazelas's user avatar
-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,...
Gurjeet Singh's user avatar
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 && ...
Stéphane Chazelas's user avatar
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/...
Bodo's user avatar
  • 6,138
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). ...
Michal's user avatar
  • 31
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>
Romeo Ninov's user avatar
  • 18.1k
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/(...
Chris Davies's user avatar
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 ...
Stéphane Chazelas's user avatar
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 ...
rr0ss0rr's user avatar
  • 362
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 &...
jesse_b's user avatar
  • 38.1k
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 ...
user11879102's user avatar
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&...
kitiyo's user avatar
  • 1

Top 50 recent answers are included