19 votes

This 'while' loop countdown with sleep doesn't work

SECONDS is a variable that's used internally by Bash, and the way this variable works is interfering with your script's decrement action. If you use a different variable name (like SECS or seconds) ...
Sotto Voce's user avatar
  • 3,963
17 votes
Accepted

Can a script be run as a subshell?

(. ./myscript.sh) is the right way (the standard form is ., not source, and . with no directory specified searches on the PATH). Doing this using a shebang would require having a reliable way of ...
Stephen Kitt's user avatar
14 votes
Accepted

Looking for a more permissive alternative to GNU getopt for wrapper script

A common method for tasks like this is to use -- as a separator between options to be handled by the wrapper script and options to be passed on verbatim to the program being executed by the wrapper, e....
cas's user avatar
  • 77.7k
10 votes

This 'while' loop countdown with sleep doesn't work

As @SottoVoce said, $SECONDS is a special variable in bash and other Korn-like shells. In bash and ksh (not zsh), unsetting the variable as with unset -v SECONDS removes its special meaning, but in ...
Stéphane Chazelas's user avatar
10 votes
Accepted

Update crontab rules without overwriting or duplicating

From what I can tell, there's no way to tell crontab to refer to a set of files to collate the rules (without duplication or overwriting)/ There is, but not for user crontabs. Use individual files in ...
muru's user avatar
  • 71.3k
7 votes
Accepted

Feed function specific files from the directory

#!/bin/sh set -- *_file.txt until [ "$#" -lt 2 ]; do process "$1" "$2" >"output_${1%_file.txt}${2%_file.txt}" shift 2 done This sets the ...
Kusalananda's user avatar
  • 327k
7 votes

Feed function specific files from the directory

You could do this with the xargs command. If I have these files: $ ls A_file.txt B_file.txt C_file.txt D_file.txt E_file.txt F_file.txt G_file.txt H_file.txt Then I can process these two at a ...
larsks's user avatar
  • 33.2k
7 votes

Sed based on 2 words, then replace whole line with variable

To let the c command only affect lines that contain both banana and wait, make sure to skip all lines that don't match these two patterns. You may do this using the b command, which will branch to ...
Kusalananda's user avatar
  • 327k
7 votes
Accepted

Race condition not seen while two scripts write to a same file

Simply, the echo command triggers one write syscall which is atomic. Note that write doesn’t guarantee to write all bytes it is given, but in this case (few data), it does. Then in theory write(fd, ...
Frédéric Loyer's user avatar
6 votes

AWK reading path from text file

You could build the same logic with the print/no-print flag inside the while(getline) loop. Something like this: awk '{ while((getline a < $0) > 0) { if (a ~ /BEGIN/) p=1; if (p) print ...
ilkkachu's user avatar
  • 135k
6 votes
Accepted

SSH session termination on script error when sourcing multiple sub-scripts

The reason this is happening is because you are sourcing the master script. I can reproduce the behavior you describe using these two toy examples: terdon@oregano ~ $ cat ~/scripts/foo.sh #!/bin/bash ...
terdon's user avatar
  • 239k
5 votes

Feed function specific files from the directory

If switching from bash to zsh is an option, then it's just: for i j ( *.txt(N) ) some_program -- $i $j > output_$i[1]$j[1] (N) enables nullglob from that one glob expansion so as not to report an ...
Stéphane Chazelas's user avatar
5 votes

How to extract part of URL from curl response in bash script?

With GNU grep: $ grep -oP 'https://[^/]+/\d+/\K.*' <<< 'https://www.example.com/1234/text/111?x=999988' text/111?x=999988 Or With Perl: $ perl -nE 'say $& if m@https://[^/]+/\d+/\K.*@' &...
Gilles Quénot's user avatar
5 votes
Accepted

How to pass string from bash to python function

Make it a shell function instead. Paste this into your terminal, or add to your shell's configuration files (e.g. ~/.bashrc for bash) pipeline_test(){ python -c 'from sys import argv; from ...
terdon's user avatar
  • 239k
5 votes
Accepted

Is it a good idea to put "export BASH_ENV=~/.bashrc" in my .bashrc?

IMO, no it's not a good idea. You'd be better off explicitly sourcing your library of functions and variables (aliases don't work in non-interactive shells unless the expand_aliases shell option is ...
cas's user avatar
  • 77.7k
5 votes
Accepted

How to make the (N) patterns of the zsh eatable by bash?

From zsh_expn: N sets the NULL_GLOB option for the current pattern So, the bash way would be to set the nullglob option for that pattern. ( shopt -s nullglob || setopt NULL_GLOB && ...
Marcus Müller's user avatar
4 votes

Problem with getting unique output in script

If you have GNU grep, the default on Linux, you could simplify with : extract_parts(){ grep -oP 'https?://\K[^/]+' "$1" | sort -u } Output $ extract_parts wget-list downloads....
Gilles Quénot's user avatar
4 votes

what are practical uses of stdbuf or nohup?

Let's start with an example without stdbuf. You need 2 terminals because tail -f will block. Terminal 1 touch log_file1 tail -f log_file1 | cut -d " " -f 1 | uniq Terminal 2 echo "A 1&...
Franz Wong's user avatar
4 votes
Accepted

Problem with getting unique output in script

while read a; do You're reading one line... host=$(echo -e "$a" | awk -F '/' '{print $1}' | sort -u) and printing that in a pipeline which you then sort and take the unique lines out of. ...
ilkkachu's user avatar
  • 135k
4 votes

AWK reading path from text file

Something like the following, untested, will do what you want using any awk: awk ' NR == FNR { ARGV[ARGC++]=$0; next } $1 == "BEGIN" { f=1 } f $0 == "END BEGIN" { f=...
Ed Morton's user avatar
  • 30.2k
4 votes

Shell script: How to prepend env variable with prefix only if environment variable is defined?

command ${VALUE:+--flag "$VALUE"} This runs command with the option --flag and its option-argument "$VALUE" if the variable VALUE is defined and non-empty. If the variable is ...
Kusalananda's user avatar
  • 327k
4 votes
Accepted

How to make my script stay executable on different devices?

When I copy the script to a flash drive and then copy it from a flash drive to another computer running the same operating system I have to make it executable again to give back the function to right ...
xiota's user avatar
  • 526
4 votes

Update crontab rules without overwriting or duplicating

You can use serie of command to accomplish this. Here is sample script to do it: #get current cron crontab -l >cron_c #add new lines echo "0 * * * * command" >>cron_c #remove ...
Romeo Ninov's user avatar
  • 17.2k
4 votes

How to make the (N) patterns of the zsh eatable by bash?

If it's in a zsh-specific part, you can avoid bash complaining upon parsing that code that it will never run by doing: eval 'cmd this_pattern*(N)' instead of cmd this_pattern*(N) That eval command ...
Stéphane Chazelas's user avatar
4 votes
Accepted

How and why does using redirection or writing files within an if statement affect exit code?

$? In the PARAMETERS SET BY THE SHELL section of man zshparam, or info zsh 'Parameters Set By The Shell' you'll see that, $? is the exit status returned by the last command. $(...) In man zshexpn or ...
aviro's user avatar
  • 4,025
4 votes

How to improve script for redacting and copying a file

First off, always quote your variables and this goes double if they store file names, since they can have any character except NUL (\0) and /. You're also doing some unnecessary movement and creation ...
terdon's user avatar
  • 239k
3 votes

While working on a file in a gui application, how to quickly open the parent directory of a file in the file browser?

Please check Open File Explorer on a File that is open by another Application. I am the OP of that question. menu's answer is the accepted one. Here is the complete solution. However, to understand ...
Ahmad Ismail's user avatar
  • 2,560
3 votes

How do I resolve a relative path in a POSIX shell if readlink/realpath is not available?

There is a realpath C function, though. So maybe one of the other utilities uses it secretly? From the top of my head, I'm not aware any of those utilities reference the realpath function in their ...
hakre's user avatar
  • 431
3 votes
Accepted

how to interpret service command output

Instead of looking at the output, you should check service’s exit code: if service apparmor status >&/dev/null; then echo AppArmor is loaded and enabled; fi This needs to run as root to ...
Stephen Kitt's user avatar
3 votes

Can a script be run as a subshell?

POSIX (IEEE Std 1003.1-2017, Section 2.12) specifies: A subshell environment shall be created as a duplicate of the shell environment, except that signal traps that are not being ignored shall be set ...
Zaidhaan Hussain's user avatar

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