New answers tagged

1

I merge my previous code with hints provided by @Oh My Goodness. I excluded the variable clr_dir. The code thanks to is much more readable. And I moved temp dir to the same filesystem as is final destination of new CRL. #!/usr/bin/env bash set -euo pipefail cd /etc/pki/tls/misc crls=(RootCA IntermediateCA IntermediateCA2) crl_temp=$(mktemp -p .) for ...


6

for crl in ${crls[@]}; do It's a good habit to always double-quote array expansions. Cleaning up the temp directory after a successful run would be a nice touch (or do away with it altogether; see below). cat ${crl_temp}/*.pem > ${crl_temp}/${new_crl} mv ${crl_temp}/${new_crl} ${crl_dir}/${new_crl} I assume you're doing this to get atomic replacement? ...


5

bash is slow to append and index into strings: both operations are \$O(n)\$. Your approach does as many indexes and appends as there are digits in the input, making it \$O(n^2)\$. And processing a string by chomping the first character repeatedly will be \$O(n^2)\$ in almost every language. Have a look at these timings for large inputs, using your code: ...


3

With this code and your example data, both a/c/0.png and b/0.png get renamed to 1_0.png. This happens because changes to $n inside ( ) get lost when you leave the subshell. Use bash's "globstar" feature to recurse for you, and assign numbers as normal. **/ matches all subdirectories. You want the current directory too, so add . to the list. This doesn't ...


4

Limit globbing to directories only by appending a slash: for dir in */ If you cd into subdirectories, you don't need to construct a new path for each file. If you cd inside a subshell with ( cd … ), the original directory will be restored when subshell exits. Make sure to increment n outside of the subshell, or the new value will be lost! The IFS= is ...


0

Note: a module is actually a directory located in some "wp-content/plugins" directories. Study case Your shell command is incorrect, inefficient and unnecessarily convoluted. for i in $(find . -type d -name 'plugins' | grep 'wp-content/plugins$'); do find $i -maxdepth 1 -type d -exec sh -c 'for f do basename -- "$f"; done' sh {} + ; done | sort -u The ...


1

Variables don't interpolate inside single quotes. You need double quotes. No need to capture the old version that we're throwing away. The /g is redundant since the pattern can never match more than once per line (being anchored to start-of-line). What happens with Version: 0.9a? Version:1? Version:? Better to take the whole line, possibly excluding ...


3

If plugins only contain directories, list every unique entry in plugins: shopt -s extglob \ls -- **/wp-content/plugins |sort -u Otherwise, include the final targets in the glob, plus a trailing slash to limit globbing to directories. That will yield paths relative to ., with trailing slash like siteA/wp-content/plugins/anotherplugin/. We clean it up ...


1

Just maybe rsync for remote synchronisation might be attractive. rsync --dryrun ...


0

I don't think you need the temporary file, if you open for append (i.e. >>), and use comm to find the non-duplicate lines (after sorting).


4

There's nothing here that requires Bash rather than standard (POSIX) shell, so we can use #!/bin/sh However, don't do that, because we can use a Bash feature to avoid exposing the plaintext password to other users (see below). I recommend making the shell exit if we attempt to expand any unset variables, or if any of the programs we run exits with an ...


3

Try to avoid using echo in your bash script; use printf instead (why? look here): hashed=$(printf "%s" "$GUAC_PASSWORD" | sha256sum | cut -f1 -d\ ) Note also the uppercase variable in bash script are for environment variables; use lowercase ones (ref). At last, do not use <<< in your case because it adds a newline and won't output the same hash ...


6

If GUAC_PASSWORD is a string like -e foo or /etc/*, that's going to create problems. Quote the input and avoid echo altogether. While you're at it, whitelist the checksum output, making head redundant: HASHED=$( tr -d '\n' <<<"$GUAC_PASSWORD" | sha256sum | tr -dc a-f0-9 ) edit: suppress newline added by <<<


2

Use sort's -u and -o switches: find … | sort -u - $file -o $file


1

I recommend starting the script with a shebang so it can be executed as a program: #!/bin/bash As there's nothing in here that isn't standard shell, then we can make it leaner by using plain POSIX shell instead: #!/bin/sh I recommend set -eu, to cause the shell to abort in more error cases. We can save one fork by using exec for the final command. In ...


1

Well, to be honest I am not exactly sure what do you want to review :) This isn't a script. It is just standard zip command. Maybe you should think about this: ${war}/mediawiki_general_backups/${domain}-directory-backup-${date}.zip. It is too long and ugly. Try to add more these things into your script and a readability will be lost. I fear I don't have ...


1

I like the script. But I have maybe some suggestions for improvement. This part: RAM=$(free -m) total=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $2}') available=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $7}') Personalty I prefer to dig these values from /proc/meminfo to avoid running unnecessary free utility, but I saw several scripts based on it. And in case ...


3

Naming Try not to use generic names like $file. Make your code read easy for others. Say $files_to_backup for example. Check for errors when it matters If backup is important: check that the backups succeeded: if cp "${files[@]}"${war}/mediawiki_specific_backups";then rm -rf "${war}/${domain}" mkdir "${war}/${domain}" ... ... else #...


Top 50 recent answers are included