Skip to main content
Share Your Experience: Take the 2024 Developer Survey

New answers tagged

0 votes

Can I reuse a loop variable for a background task in a loop in bash?

FYI when you do b "$i" & the shell will already run b in a background process, you don't need to add yet another process with ( b "$i" ) &. To answer your question of "...
Ed Morton's user avatar
  • 32.1k
1 vote

bash: what's the difference between '< file' and taking input from a here-string which contains the file?

When one reads the most recent manual on openssl (openssl v3.3 man1 openssl-rsa ) (and know a bit about the background of openssl), one can see that openssl assumes your keys to reside in files. With ...
humance's user avatar
  • 11
0 votes

How to diff two files in bash efficiently?

#!/bin/bash # Function to sort string lists within braces sort_string_lists() { local file="$1" local matchedBraces=$(grep -o '\{[^}]*\}' "$file") while read ...
user611494's user avatar
0 votes

how to columnate list of strings in two lines, aligned to the length of the first column

Using Miller (mlr) to convert the input from a pipe-delimited headerless CSV format to "pretty-printed" tabular format, while also prepending the last field with a newline and four spaces: $ ...
Kusalananda's user avatar
  • 337k
0 votes

how to columnate list of strings in two lines, aligned to the length of the first column

Solution in TXR: python2-distutils-extra => python2-distutils-extra 2.39-5 Enhancements to the Python build system python2-fuse => python2-fuse 1.0.5-1 This is a Python 2 ...
Kaz's user avatar
  • 8,466
-3 votes

$@ except the 1st argument

$ cat ./test.sh PARAMS="$@" echo "${PARAMS#$1 }" $ ./test.sh ONE TWO THREE TWO THREE $ ./test.sh ONE TWO THREE FOUR FIVE SIX TWO THREE FOUR FIVE SIX $ ./test.sh ONE TWO THREE ...
Luis Santos's user avatar
0 votes

Bash script: echo output of variable and write to file

#!/bin/bash # see: man grep # -q makes grep not give any output # -m1 makes grep stop reading after finding the first match # using cat in stead of cp to preserve file test01's mode and owners if ...
humance's user avatar
  • 11
6 votes
Accepted

Can I reuse a loop variable for a background task in a loop in bash?

No, the values would not interfere as each command will run in a separate subshell. Also, double quote the array and the variable to prevent word splitting. for i in "${a[@]}" ; do ... ...
choroba's user avatar
  • 47.4k
2 votes
Accepted

Add a numerical value to a variable while reading a file in bash in loop

value=$ret+1095 is not an arithmetic assignment, and the bash shell has no print (perhaps you meant printf?). You could do while IFS=, read -r line ret; do let value=$ret+1095 echo "$line,$...
steeldriver's user avatar
  • 81.5k
0 votes

Debian network namespace isolation

Creating a new namespace is done with the ip netns command eg ip netns add newnamespace In order to let the namespace talk to the rest of the network it needs a veth endpoint inside the namespace. ...
Stephen Harris's user avatar
1 vote

How to display all existing regular files from a text file using one-liner?

With zsh: print -rC1 -- ${(f)^!$(<file.txt)"}(N) Would print those of those files that can be found regardless of their type. print -rC1 -- ${(f)^!$(<file.txt)"}(N.) To limit to ...
Stéphane Chazelas's user avatar
3 votes

Do I have a cleaner way to assign a parametrized json string to a bash variable?

You're currently creating a string that's a mixture of format (names of fields, structure chars like { and [, etc.) and data (arguments to the function). I'd separate the data from the format and use ...
Ed Morton's user avatar
  • 32.1k
1 vote

how to columnate list of strings in two lines, aligned to the length of the first column

Using any POSIX awk: $ cat tst.awk BEGIN { FS="|"; OFS="\t" } { gsub(/^[[:space:]]+|[[:space:]]+$/,"") wid = ( length($1) > wid ? length($1) : wid ) vals[...
Ed Morton's user avatar
  • 32.1k
13 votes

Do I have a cleaner way to assign a parametrized json string to a bash variable?

Using jq to create pieces of JSON for each sub-element of the top-level payload document and then putting everything together. This way, you ensure that jq has an opportunity to encode every string, ...
Kusalananda's user avatar
  • 337k
12 votes
Accepted

Do I have a cleaner way to assign a parametrized json string to a bash variable?

Yes, there is a way that's cleaner for many types of JSON documents (though not all types), a here document: local pet_to_add=$(cat - <<END_DOC { "id": $id_pet, "...
Sotto Voce's user avatar
  • 4,377
3 votes

Is it possible to read from stderr in a bash script?

No, this is not possible. Your attempt doesn't work for a fundamental reason: stderr is an attribute of each process, not some system-wide thing. Standard error (stderr) just means file descriptor 2 ...
Gilles 'SO- stop being evil''s user avatar
-1 votes

Is it possible to read from stderr in a bash script?

The documentation, man bash has this to say about the read command read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...] One line is read ...
Chris Davies's user avatar
0 votes
Accepted

convert list of word in github actions into json array and use as a strategy matrix

You could use jq - either treating standard input as raw input $ echo hello1 hello2 hello3 | jq --raw-input 'split(" ")' [ "hello1", "hello2", "hello3" ] ...
steeldriver's user avatar
  • 81.5k
4 votes
Accepted

How to display all existing regular files from a text file using one-liner?

The starting point for this could be a simple loop: while IFS= read -r item do [ -f "$item" ] && printf '%s\n' "$item" done <file.txt This could be crashed onto a ...
Chris Davies's user avatar
3 votes

How to display all existing regular files from a text file using one-liner?

Here's one way: while IFS= read -r file; do [ -f "$file" ] && printf '%s\n' "$file" done < file.txt And sure, you can stick it on one line, if you prefer: $ while IFS=...
terdon's user avatar
  • 243k
1 vote

How in convenient and flexible way to input time period for timer and convert it to seconds in Bash?

In zsh instead of bash, you could do something like: function t2s time2seconds { set -o{localoptions,extendedglob,histsubstpattern} local s=1 m=60 h=60*m d=24*h w=7*d spec=${(j[ ])argv:l} spec=$...
Stéphane Chazelas's user avatar
1 vote

How in convenient and flexible way to input time period for timer and convert it to seconds in Bash?

Consider researching performance improvements, especially in terms of eliminating all the expensive subshell invocations. Eliminate a single subshell invocation by replacing the pipes with here-...
markp-fuso's user avatar
-1 votes

How in convenient and flexible way to input time period for timer and convert it to seconds in Bash?

Implementation below in time2seconds function. Usages examples: time2seconds 1h30m50s debug # 5450 t2s 1h30m-30s d # 5370 t2s 1h-5s d # 3595 t2s 15m*3 d # 2700 log(){ msg_content="$1&...
Anton Samokat's user avatar
9 votes
Accepted

printf in Zsh does not shell escape exclamation mark

It looks like it checks the current shell settings to determine if the bang is special. History expansion isn't enabled by default in noninteractive shells, so it's not special in a script. % (setopt ...
ilkkachu's user avatar
  • 140k
0 votes

Bash script if find something copy this file

I am assuming that what you want is to get the names of all files whose size is greater than 12k, and then grep all files in the local directory for that file name, appending the output to input.xml. ...
terdon's user avatar
  • 243k
3 votes
Accepted

How to make mv work with exotic directory names?

I can't be sure here since I don't know what the output of this "notmuch" command is. However, as a general rule, you should avoid using for loops for things like this. See bash pitfall ...
terdon's user avatar
  • 243k
1 vote
Accepted

Write header as first line at end of data

The POSIX stdin(3) manual page states: [...] the standard input and standard output streams are fully buffered if and only if the stream can be determined not to refer to an interactive device. ...
Vilinkameni's user avatar
0 votes

help with bash script to [grep sed cut] smartctl output to desired format

Here's an extensible awk solution: # For each MegaRAID device... for m in $(smartctl --scan | grep megaraid | cut -d, -f2) do # Scan it and report the index, product, and serial number ...
Chris Davies's user avatar
2 votes
Accepted

Shell: How to print all files in a folder with cat with files separator

If you have GNU sed, you can append some line to the end of every file, for instance: sed -s '$a--- END OF FILE ---' -- * The -s or --separate flag is to "consider files as separate rather than ...
aviro's user avatar
  • 5,632
1 vote
Accepted

help with bash script to [grep sed cut] smartctl output to desired format

This will extract a single drive’s product name and serial number, in your loop: (echo "$m"; smartctl -a -d "megaraid,$m" /dev/sda | grep -Po '^(Product|Serial number):\s*\K.*') | ...
Stephen Kitt's user avatar
2 votes

Looking for assistance in expect

This expect code does not send the password multiple times. If the password is incorrect and the remote host prompts for the password again, then the expect code will wait 5 seconds to see the ...
glenn jackman's user avatar
2 votes

help with bash script to [grep sed cut] smartctl output to desired format

For what you're doing, I think trying to parse the output of smartctl --scan is the wrong solution. If you want a list of model and serial number for all attached drives, you could do something like: ...
larsks's user avatar
  • 35.4k
4 votes
Accepted

Append the arguments to a script as lines in a file

In zsh, you can do: print -rC1 -- "$@" Prints the arguments raw on 1 Column. Add >> file to append that output to a file. print is ksh's replacement for the highly unreliable and ...
Stéphane Chazelas's user avatar
0 votes
Accepted

how to replace the last n occurences of string using sed

Although there is no direct sed option for this operation, here is a practical approach: replace the last occurrence of the string with: $ sed -E 's/(.*)OLD/\1NEW/' input_file As n is usually small ...
dhm's user avatar
  • 1,624
0 votes

Bash script: echo output of variable and write to file

So, it looks like that you want to copy test02.txt to test01.txt if test01.txt has one and only one line that contains A and that line is A, B, C, D. Then maybe that should be something like: #! /bin/...
Stéphane Chazelas's user avatar
1 vote

Run a command that is shadowed by an alias

One way that should be portable and independent of a shell type is to use env: env - run a program in a modified environment SYNOPSIS env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...] So ...
Petr's user avatar
  • 1,674
0 votes

Question about home directory permissions script

/etc/init.d/ssh uses chmod in function check_privsep_dir(), which creates the PrivSep empty directory if it's missing. This is related to logins, like the other three changes that you show; it's not ...
klode's user avatar
  • 64
1 vote

How to place a qemu vm in background (it means that it should run between the processes but nothing should be shown on the screen)

This seems to work for me: qemu ... -nographic -serial none -monitor none & But I would probably instead use a network serial console so that I can still access the console if necessary: qemu ... ...
larsks's user avatar
  • 35.4k
1 vote
Accepted

Sorting Bash History for Redundancy Removal

Given 1 line per command as in your posted sample input, this Decorate-Sort-Undecorate approach will work using any awk, sort, etc.: $ cat tst.sh #!/usr/bin/env bash tmp1=$(mktemp) || exit 1 tmp2=$(...
Ed Morton's user avatar
  • 32.1k
0 votes

Determining name of the variable passed to function

To output an export statement with or without a value depending on whether a value was given to the function: write_export () { printf 'export %s\n' "$1${2+=${2@Q}}" } This writes the ...
Kusalananda's user avatar
  • 337k
0 votes

Setting PS1 with site-wide configuration

If you're willing to force clients to run their shell in a tmux session, you could spawn a tmux session at the end of /etc/profile, before ~/.bashrc is sourced, colorizing the bottom bar, so that any ...
kos's user avatar
  • 3,172
3 votes
Accepted

Setting PS1 with site-wide configuration

If your users set their preferred $PS1 in their ~/.bashrc but do not set the $PROMPT_COMMAND variable, you could add: if [[ -n $SSH_CLIENT$SSH_TTY ]]; then PROMPT_COMMAND=' if [[ $PS1 != *SSH* ]]...
Stéphane Chazelas's user avatar
3 votes

how to de-duplicate block (timestamp+command) from bash history?

You didn't show your attempt in awk, but the following awk program prints entries in the sense of #[number] [command consisting of one or more lines] where the command is unique. The program is: # ...
Vilinkameni's user avatar
1 vote
Accepted

how to de-duplicate block (timestamp+command) from bash history?

Using Perl, you may do: perl -ge ' @u = map { $c = $_; $c =~ s/^#[0-9]{10}\n//; exists($d{$c}) ? () : ($d{$c} = 1 && $_) ; } &...
kos's user avatar
  • 3,172
1 vote

How do I make Debian's greeting look more like Ubuntu's?

You appear to have access to both debian and Ubuntu - you have access to the answers. The content is shown by the MOTD mechanism which simply cats a file to the terminal. This is obviously dynamic ...
symcbean's user avatar
  • 5,631
1 vote

how to replace the last n occurences of string using sed

Using GNU awk for patsplit(): $ echo 'OLDOLDOLDOLDOLD' | awk ' { nf = patsplit($0,f,"OLD",s) out = s[0] for ( i=1; i<=nf; i++ ) { ...
Ed Morton's user avatar
  • 32.1k
0 votes

bash script with rwx bits set, but still permission denied

There are two easy reasons for this /bin/bash exists but is not executable. Or even it loader isn't executable. If you can run /bin/bash directly, this isn't the problem. The mount point that the ...
David G.'s user avatar
  • 1,483
0 votes

How to make bc handle explicitly positive numbers?

As an alternative to bc simple calculations can be done with built-in bash calculator — Arithmetic Expansion $((expression)): expr=" +1++1-+1--1+-1-1 + 1 + + 1 - + 1 - - 1 + - 1 -1 "; echo $...
Anton Samokat's user avatar
0 votes

How to make bc handle explicitly positive numbers?

bc by now does not support unary plus operation for digits. Also it considers ++ and -- operations as increment and decrement though they are not supported for digits. This is for cases for both plus ...
Anton Samokat's user avatar
2 votes
Accepted

How to make bc handle explicitly positive numbers?

Try this: expr='+1++2' echo "0+ $expr" | sed 's/+ *+ */+/g' | bc You can use a shell like bash directly but it's a little cumbersome and possibly not as robust: expr='+1++2' _tmp="0+ $...
Chris Davies's user avatar

Top 50 recent answers are included