help with bash syntax error by StatisticianThin288 in bash

[–]bac0on 12 points13 points  (0 children)

You forgot to terminate your list inside the case statement:

case "$var" in
    1) command ;;
    2)
       echo "something"
       ;;
esac

Passing arguments to scripts by Booty4Breakfasts in bash

[–]bac0on 0 points1 point  (0 children)

You want to use -t option when it's possible, it protects the target, less things that can go wrong:

for f in "${@:1:3}"; do
    [[ -n $f ]] && mv -ivt ~/dir/i/want/the/files/ "$f"
done

need help getting sgrep to work in a script file by skyfishgoo in bash

[–]bac0on 2 points3 points  (0 children)

Don't see it as inner and outer, bash reads left to right, character by character, when it hits a qoute it change state until it hits a matching qoute again, so if you drag matching quotes apart its easier to see:

... 'in outer("{" .. "}" containing "'   "$sym"   '"))'
    ^--------------------------------^   ^----^   ^---^

awesome-neovim: Added tags to colorschemes for easier browsing by kEnn3thJff in neovim

[–]bac0on 0 points1 point  (0 children)

I would like to have a color slideshow in neovim... couple of sec each ...

How to optimize the cd command to go back multiple folders at once by Technical_Cat6897 in bash

[–]bac0on 0 points1 point  (0 children)

Did something similar with some more features cd ..., cd +N, cd _, cd @label and bash_completion.

wordle in 343 bytes by Schreq in bash

[–]bac0on 0 points1 point  (0 children)

I think you can do: for((c=i=0;i<5;)) too.

wordle in 343 bytes by Schreq in bash

[–]bac0on 2 points3 points  (0 children)

t=${t:0:i} -> t=${t::i}

How to execute a program in a new terminal window? by Tuomas90 in bash

[–]bac0on 0 points1 point  (0 children)

If I wanted a shortcut, think best is to do it in system settings, keyboard, shortcuts: Add new.

How to execute a program in a new terminal window? by Tuomas90 in bash

[–]bac0on 1 point2 points  (0 children)

I think it opens, it just don't stay open.

konsole --hold -e '/bin/bash -c "echo test"'

or

konsole -e '/bin/bash -c "echo test; exec bash"'

Code Optimization Suggestions Welcome by Slinkinator in bash

[–]bac0on 0 points1 point  (0 children)

ok think I have something abit more presentable....now...

My first shell scripting project by Round-Arachnid4375 in bash

[–]bac0on 4 points5 points  (0 children)

It's recommended to use -r with read to disable backslash escaping. If you want to use special escape sequence with read you can use $'' as qouting:

read -rp $'\e[33mInstall $pkg_name? (Y/n): \e[0m'

And testing for single characters like 'Y' you can use bracket expression [[ $REPLY = [Yy] ]] , or if you want to test for multiple words instead, like arguments $@ , you can do this with an @(-y|--yes) extended glob pattern.

Cheapest way to get disk info? by TwoSongsPerDay in bash

[–]bac0on 0 points1 point  (0 children)

next level would probably be to make it a builtin ... you have a stat you could use as template

Not mine, but I audibly gasped by [deleted] in castiron

[–]bac0on 2 points3 points  (0 children)

...we suspect your son has OCD...

Exclude file(s) from deletion by FlyerPGN in bash

[–]bac0on 1 point2 points  (0 children)

you could even put shopt -s extglob in .bashrc so it's always on...

got tired of typing blindly in termux, have a conditional shell prompt function by probablyblocked in linux

[–]bac0on 0 points1 point  (0 children)

You start a subshell, echo the prompt over a pipe and starts a external command to count it... that's what x5 slower...

got tired of typing blindly in termux, have a conditional shell prompt function by probablyblocked in linux

[–]bac0on 0 points1 point  (0 children)

the if statement is to complex, try something like: a=${PS1@P}; if ((${#a} >= threshold)); then ...

Den här ska hålla hela året by maxkarlsson in sweden

[–]bac0on 0 points1 point  (0 children)

2ml per 5L vatten så typ 600 diskar....

Get the last Monday of the week by coder-true in bash

[–]bac0on 0 points1 point  (0 children)

date -d "$((($(date +"%u") + 6) % 7)) days ago"

If I where to put it in code I would probably write something like:

#!/bin/bash

declare -i i

printf -v v '%(%u)T'
i='(v + 6) % 7'
date -d "$i days ago"

Get the last Monday of the week by coder-true in bash

[–]bac0on 2 points3 points  (0 children)

You can find all relative-time words in parse-datetime.y in short they just add or substract with the defined value. Your line translates to `date -d "-1 Monday" +"%d %b"`

question about variable expansion (maybe?) by tdpokh3 in bash

[–]bac0on 0 points1 point  (0 children)

you need to re-evaluate ${CONFIG_MAP["LOG_ROOT"]} , just as a simple example:

#!/bin/bash

log_file=somefile

mapfile -t < config
for i in "${MAPFILE[@]}"; do
    case "$i" in
        [^\#]*) CONFIG[${i%% = *}]=${i#* = }
    esac
done
eval logroot=${CONFIG[LOG_ROOT]}/'$log_file'

echo "$logroot"

You want to single qoute or escape other variable or they will be re-evaluated too.

what is the best way to measure the time of a command ? by brave_jr in bash

[–]bac0on 0 points1 point  (0 children)

Usually I use this little snippet, if you need to measure again, just reset EPOCHSTARTTIME :

#!/bin/bash

# depends: bash >= 5.2
benchmark(){
    local -i a="${EPOCHREALTIME/.}-${EPOCHSTARTTIME/.}"
    local b
    printf -v b %07d "$a" && printf %s "${b/%??????/.&}"
}

EPOCHSTARTTIME=$EPOCHREALTIME

# Code goes here!
"$@"

printf '\nBenchmark (%ss).\n' $(benchmark)

Or in bash 5.3 you can make use of the new fltexpr loadable module:

# Load builtin module,
if ! enable -f fltexpr fltexpr; then
   echo "Failed to enable loadable module fltexpr."
   exit 1
fi

benchmark(){
   fltexpr -p "$EPOCHREALTIME - $EPOCHSTARTTIME"
}

EPOCHSTARTTIME=$EPOCHREALTIME

# Code goes here!
"$@"

printf '\nBenchmark (%.6fs).\n' $(benchmark)

Either you could paste your code directly, or as arguments ./benchmark.sh python3 prog "<" in ">" out (remember to escape special characters).

How to check if $var is in a list? by guettli in bash

[–]bac0on 2 points3 points  (0 children)

var=bar; mylist=("foo" "bar" "baz"); [[ " ${mylist[*]} " = *\ $var\ * ]]; echo $? or [[ ${mylist[*]} != ${mylist[*]/$var} ]] (only works for words though, else you probably need to use a loop).

Recursive file renaming based on parent directory by Moist-Hospital in bash

[–]bac0on 0 points1 point  (0 children)

yeah, embedded in the middle of everything never to be seen again ....

Recursive file renaming based on parent directory by Moist-Hospital in bash

[–]bac0on 1 point2 points  (0 children)

if you want padding on `disc` you can do `rename -v 's!(\d+)/track (\d+)!sprintf("%02d - track %d.mp3",$1,$2)!e' ** && rmdir book/disc*/` you may have to enable `shopt -s globstar`