# split directories up into nesting levels relative to base search directory
# is base search directory is '/a/b'; then: level 0 is '/a/b', level 1 is '/a/b/_', level 2 is '/a/b/_/_, level 3 is '/a/b/_/_/_' etc.)'
eLevels[0]="${bdir}";
for nn in "${edir[@]%%+([\/\*])}"; do
b="${nn#"${bdir%/}/"}/";
a="${bdir%/}/";
kk=1;
until [[ -z $b ]] || [[ "$a" == "$nn" ]]; do
a+="${b%%/*}/";
b="${b#*/}";
eLevels[$kk]+="${a%\/}"$'\n';
((kk++));
done;
done;
# construct minimal list of files/directories to search that doesnt contain excluded directories and save in array F
# EXAMPLE:
# if the base search directory is '/a/b' and you want to exclude 'a/b/c', 'a/b/d/e', and 'a/b/d/f', this includes:
# everything immediately under '/a/b' except for '/a/b/c' and '/a/b/d' --AND--
# everything immediately under '/a/b/d', except for '/a/b/d/e' and '/a/b/d/f'
mapfile -t F < <(for ((kk=1; kk<${#eLevels[@]}; kk++ )); do
mapfile -t A < <(printf '%s' "${eLevels[$(( $kk - 1 ))]}" | sort -u)
A=("${A[@]}");
for nn in "${A[@]}"; do
mapfile -t B < <(printf '%s' "${eLevels[$kk]}" | grep -F "${nn}" | sort -u)
B=("${B[@]}");
[[ -n "$(printf '%s' "${B[@]//[ \t]/}")" ]] && source /proc/self/fd/0 <<< "find \"${nn}\" -maxdepth 1 -mindepth 1 $(printf '! -path "%s" ' "${B[@]}"; printf '! -wholename "%s/*" ' "${B[@]}")";
done;
done);
# run `find -O3` on the dir list saved in array F, with excluded files (from command line) now being excluded
source /proc/self/fd/0 <<<"find -O3 \"\${F[@]}\" $(printf '! -path "%s" ' "${efile[@]}")";
}
https://redd.it/1abct5q
@r_bash
# is base search directory is '/a/b'; then: level 0 is '/a/b', level 1 is '/a/b/_', level 2 is '/a/b/_/_, level 3 is '/a/b/_/_/_' etc.)'
eLevels[0]="${bdir}";
for nn in "${edir[@]%%+([\/\*])}"; do
b="${nn#"${bdir%/}/"}/";
a="${bdir%/}/";
kk=1;
until [[ -z $b ]] || [[ "$a" == "$nn" ]]; do
a+="${b%%/*}/";
b="${b#*/}";
eLevels[$kk]+="${a%\/}"$'\n';
((kk++));
done;
done;
# construct minimal list of files/directories to search that doesnt contain excluded directories and save in array F
# EXAMPLE:
# if the base search directory is '/a/b' and you want to exclude 'a/b/c', 'a/b/d/e', and 'a/b/d/f', this includes:
# everything immediately under '/a/b' except for '/a/b/c' and '/a/b/d' --AND--
# everything immediately under '/a/b/d', except for '/a/b/d/e' and '/a/b/d/f'
mapfile -t F < <(for ((kk=1; kk<${#eLevels[@]}; kk++ )); do
mapfile -t A < <(printf '%s' "${eLevels[$(( $kk - 1 ))]}" | sort -u)
A=("${A[@]}");
for nn in "${A[@]}"; do
mapfile -t B < <(printf '%s' "${eLevels[$kk]}" | grep -F "${nn}" | sort -u)
B=("${B[@]}");
[[ -n "$(printf '%s' "${B[@]//[ \t]/}")" ]] && source /proc/self/fd/0 <<< "find \"${nn}\" -maxdepth 1 -mindepth 1 $(printf '! -path "%s" ' "${B[@]}"; printf '! -wholename "%s/*" ' "${B[@]}")";
done;
done);
# run `find -O3` on the dir list saved in array F, with excluded files (from command line) now being excluded
source /proc/self/fd/0 <<<"find -O3 \"\${F[@]}\" $(printf '! -path "%s" ' "${efile[@]}")";
}
https://redd.it/1abct5q
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
like grep -A4 but instead of 4 lines go until matched pattern?
Does
https://redd.it/1advlq7
@r_bash
Does
grep have a way of instead of specifying 4 like grep -A4 to print the lines until it encounters a regex match like ^---? It looks like this is called context line control in the manpage, but that section doesn't give a way to have it variable, the number must be fixed. Does grep have another mechanism that could be used? Right now I've got a python noscript that does this, but I'm very curious about a bash-1-liner. My matches can be printed properly from grep -A2 to grep -A7 to larger like -A33 and there's no way to know what the number is without counting the number of lines until the ^--- is encountered. Is grep capable of doing this on its own or do I need another tool?https://redd.it/1advlq7
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Tool for fast tables in Bash + request for design opinions
I created a table tool for high-performance data access in Bash to support a user interface tool (I'm calling it
Array Table Extension (ate)
The tool is Bash builtin written in C, and as such it can work in the noscript's process space and access elements of the noscript in which it is called, including noscript functions which are sometimes called from the tool.
I would love to find a forum to discuss several design ideas I implemented, but the
I'll appreciate any comments or insights.
https://redd.it/1adz42r
@r_bash
I created a table tool for high-performance data access in Bash to support a user interface tool (I'm calling it
pwb for Pager with Benefits) I'm finishing up:Array Table Extension (ate)
The tool is Bash builtin written in C, and as such it can work in the noscript's process space and access elements of the noscript in which it is called, including noscript functions which are sometimes called from the tool.
I would love to find a forum to discuss several design ideas I implemented, but the
ate feature about which I am soliciting opinions is one I am also considering for the pwb project is how I create a "handle" with which one accesses the ate features. I think it's pretty developer-friendly, but if I'm mistaken, I might avoid making the same mistake on the new project.I'll appreciate any comments or insights.
https://redd.it/1adz42r
@r_bash
GitHub
GitHub - cjungmann/ate: Loadable Bash builtin for using an array as a table
Loadable Bash builtin for using an array as a table - GitHub - cjungmann/ate: Loadable Bash builtin for using an array as a table
Readline parsing in command completion
Can someone help me with command completion?
Or perhaps this is more about readline library but still.
BASH_VERSION="4.4.20(1)-release"
I use this simple function to test COMP_* variables during command completion:
complete -F compvars compvars
compvars() { echo >\&2; declare -p ${!COMP_*} >\&2; return 1; }
​
And I use arguments like 'name=' and 'name=value' in my noscripts.
For example this works as I expect and COMP_WORDS is easy to use:
​
$ compvars a='' b<TAB>
declare -- COMP_CWORD="3"
declare -- COMP_KEY="9"
declare -- COMP_LINE="compvars a='' b"
declare -- COMP_POINT="15"
declare -- COMP_TYPE="33"
declare -- COMP_WORDBREAKS="
\\"'><=;|&(:"
declare -a COMP_WORDS=([0\]="compvars" [1\]="a" [2\]="=''" [3\]="b")
\^C
​
But when I use hyphens (to allow spaces in arguments) things get complicated:
​
$ compvars a='x' b<TAB>
declare -- COMP_CWORD="3"
declare -- COMP_KEY="9"
declare -- COMP_LINE="compvars a='x' b"
declare -- COMP_POINT="16"
declare -- COMP_TYPE="33"
declare -- COMP_WORDBREAKS="
\\"'><=;|&(:"
declare -a COMP_WORDS=([0\]="compvars" [1\]="a" [2\]="='" [3\]="x' b")
\^C
​
Common sense says I should get "b" as separate element in COMP_WORDS also in last completion. Why the last hyphen in COMP_WORDS[3\] doesn't also act as a word break and further split COMP_WORDS[3\] into [3\]="x'" and [4\]="b"?
Is there some solution out there to reassemble COMP_WORDS to overcome cases like this?
​
https://redd.it/1ae4rp4
@r_bash
Can someone help me with command completion?
Or perhaps this is more about readline library but still.
BASH_VERSION="4.4.20(1)-release"
I use this simple function to test COMP_* variables during command completion:
complete -F compvars compvars
compvars() { echo >\&2; declare -p ${!COMP_*} >\&2; return 1; }
​
And I use arguments like 'name=' and 'name=value' in my noscripts.
For example this works as I expect and COMP_WORDS is easy to use:
​
$ compvars a='' b<TAB>
declare -- COMP_CWORD="3"
declare -- COMP_KEY="9"
declare -- COMP_LINE="compvars a='' b"
declare -- COMP_POINT="15"
declare -- COMP_TYPE="33"
declare -- COMP_WORDBREAKS="
\\"'><=;|&(:"
declare -a COMP_WORDS=([0\]="compvars" [1\]="a" [2\]="=''" [3\]="b")
\^C
​
But when I use hyphens (to allow spaces in arguments) things get complicated:
​
$ compvars a='x' b<TAB>
declare -- COMP_CWORD="3"
declare -- COMP_KEY="9"
declare -- COMP_LINE="compvars a='x' b"
declare -- COMP_POINT="16"
declare -- COMP_TYPE="33"
declare -- COMP_WORDBREAKS="
\\"'><=;|&(:"
declare -a COMP_WORDS=([0\]="compvars" [1\]="a" [2\]="='" [3\]="x' b")
\^C
​
Common sense says I should get "b" as separate element in COMP_WORDS also in last completion. Why the last hyphen in COMP_WORDS[3\] doesn't also act as a word break and further split COMP_WORDS[3\] into [3\]="x'" and [4\]="b"?
Is there some solution out there to reassemble COMP_WORDS to overcome cases like this?
​
https://redd.it/1ae4rp4
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Simple bash noscript help
Hi,
I am hoping I can get some assistance with a simple bash noscript that I will run in a cron
If any file in one particular directory is older than 1 minute, then execute something
I cannot get find to work simply like that
any thoughts?
Thank you !
https://redd.it/1aeawaj
@r_bash
Hi,
I am hoping I can get some assistance with a simple bash noscript that I will run in a cron
If any file in one particular directory is older than 1 minute, then execute something
I cannot get find to work simply like that
any thoughts?
Thank you !
https://redd.it/1aeawaj
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
bash-workout, bash based timed workouts
Today I wrote a small utility for myself. I've been working on leveling up my bash skills recently.
This tool takes a CSV of workouts and time (in seconds) and runs through the workout allowing you to perfectly time the workout. It comes built in with rest times. If you're on MacOS it also says the workouts out loud. If you're on another form of Linux you can easily swap to using something like espeak via the `$SPEACH_COMMAND` variable.
I'm probably the only person who'll ever use this, but on the off chance you can either learn something from it or use it here it is. https://github.com/cogwizzle/bash-workout/tree/main
https://redd.it/1aeeikq
@r_bash
Today I wrote a small utility for myself. I've been working on leveling up my bash skills recently.
This tool takes a CSV of workouts and time (in seconds) and runs through the workout allowing you to perfectly time the workout. It comes built in with rest times. If you're on MacOS it also says the workouts out loud. If you're on another form of Linux you can easily swap to using something like espeak via the `$SPEACH_COMMAND` variable.
I'm probably the only person who'll ever use this, but on the off chance you can either learn something from it or use it here it is. https://github.com/cogwizzle/bash-workout/tree/main
https://redd.it/1aeeikq
@r_bash
GitHub
GitHub - cogwizzle/bash-workout: A small tool to help you run through your workout routine via bash.
A small tool to help you run through your workout routine via bash. - GitHub - cogwizzle/bash-workout: A small tool to help you run through your workout routine via bash.
Weird Loop Behavior? No -negs allowed?
Hi all. I'm trying to generate an array of integers from -5 to 5.
for ((i = -5; i < 11; i++)); do
newoffsets+=("$i")
done
echo "Checking final array:"
for all in "${newoffsets@}"; do
echo " $all"
done
But the output extends to positive 11 instead. Even Bard is confused.
My guess is that negatives don't truly work in a c-style loop.
Finally, since I couldn't use negative number variables in the c-style loop, as expected, I just added some new variables and did each calculation in the loop and incrementing a different counter. It's best to use the c-style loop in an absolute-value manner instead of using its
Thus, the solution:
declare -i viewportsize=11
declare -i viewradius=$(((viewportsize - 1) / 2))
declare -i lowerbound=$((viewradius * -1))
unset newoffsets
for ((i = 0; i < viewportsize; i++)); do
# bash can't employ negative c-loops; manual method:
newoffsets+=("$lowerbound")
((lowerbound++))
done
​
https://redd.it/1aepy2i
@r_bash
Hi all. I'm trying to generate an array of integers from -5 to 5.
for ((i = -5; i < 11; i++)); do
newoffsets+=("$i")
done
echo "Checking final array:"
for all in "${newoffsets@}"; do
echo " $all"
done
But the output extends to positive 11 instead. Even Bard is confused.
My guess is that negatives don't truly work in a c-style loop.
Finally, since I couldn't use negative number variables in the c-style loop, as expected, I just added some new variables and did each calculation in the loop and incrementing a different counter. It's best to use the c-style loop in an absolute-value manner instead of using its
$i counter when negatives are needed, etc.Thus, the solution:
declare -i viewportsize=11
declare -i viewradius=$(((viewportsize - 1) / 2))
declare -i lowerbound=$((viewradius * -1))
unset newoffsets
for ((i = 0; i < viewportsize; i++)); do
# bash can't employ negative c-loops; manual method:
newoffsets+=("$lowerbound")
((lowerbound++))
done
​
https://redd.it/1aepy2i
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Maintain list of env variables for both shell and systemd
I have a bunch of applications autostarted as systemd user services and I would like them to inherit environment variables defined in the shell config (.zprofile because these are rarely changed). I don't want to maintain two identical list of variables (one for login shell environment and one for
I thought about using systemd's
What is a good way to go about this? I suppose the shell config can be parsed for the variables but it seems pretty hacky. Associative array for env variables, then parse for the keys of the arrays for the variable names for
https://redd.it/1afawp3
@r_bash
I have a bunch of applications autostarted as systemd user services and I would like them to inherit environment variables defined in the shell config (.zprofile because these are rarely changed). I don't want to maintain two identical list of variables (one for login shell environment and one for
systemctl import-environment <same list of these variables>).I thought about using systemd's
~/.config/environment.d and then have my shell export everything on this list, but there are caveats mentioned here (I won't pretend I fully understand it all), hence why I'm thinking of just going with the initial approach (the shell config is also more flexible allowing setting variables conditionally based on more complicated logic). Parsing output of env is also not reliable as it contains some variables I didn't explicitly set and may not be appropriate for importing by systemd.What is a good way to go about this? I suppose the shell config can be parsed for the variables but it seems pretty hacky. Associative array for env variables, then parse for the keys of the arrays for the variable names for
systemctl import-environment? Any help/examples are much appreciated.https://redd.it/1afawp3
@r_bash
GitHub
Discuss: A better way to handle environment variables? · Issue #6 · alebastr/sway-systemd
This project embeds some environment variables in a shell noscript. It advises modifying the shell noscript to update the environment variables, which is not ideal. Is there a better way? Is systemd...
Video Contact Sheet
I am trying to make a video contact sheet noscript and i dont know how how to calculate the time duration of the video and pipe the input to this ffmpeg noscript.
https://redd.it/1afdtgl
@r_bash
I am trying to make a video contact sheet noscript and i dont know how how to calculate the time duration of the video and pipe the input to this ffmpeg noscript.
https://redd.it/1afdtgl
@r_bash
Imgur
Discover the magic of the internet at Imgur, a community powered entertainment destination. Lift your spirits with funny jokes, trending memes, entertaining gifs, inspiring stories, viral videos, and so much more from users.
Are there terminal apps like Fig but for windows?
I am looking for a nice-looking command line like Fig with features like autocomplete, are there windows alternatives?
https://redd.it/1affapj
@r_bash
I am looking for a nice-looking command line like Fig with features like autocomplete, are there windows alternatives?
https://redd.it/1affapj
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
What is the best way to run a server polling noscript for few days only, every 1 minute? Will it cause any hamper to server status?
host=www.google.com
port=443
while true; do
currenttime=$(date +%H:%M:%S)
r=$(bash -c 'exec 3<> /dev/tcp/'$host'/'$port';echo $?' 2>/dev/null)
if [ "$r" = "0" ]; then
echo "[$currenttime] $host $port is open" >>tori.txt
else
echo "$current_time $host $port is closed" >>tori.txt
fi
sleep 60
done
This is the noscript. It runs every 1 minute. I was planning to use setsid ./noscript.sh but I didn't find a way to exit that process easily. So, I am not doing that. Should I run it as a cronjob(It doesn't make much sense to run this as a cronjob though. as it's already sleeping for 60 seconds. Anything you can think of?)
https://redd.it/1afjbg7
@r_bash
host=www.google.com
port=443
while true; do
currenttime=$(date +%H:%M:%S)
r=$(bash -c 'exec 3<> /dev/tcp/'$host'/'$port';echo $?' 2>/dev/null)
if [ "$r" = "0" ]; then
echo "[$currenttime] $host $port is open" >>tori.txt
else
echo "$current_time $host $port is closed" >>tori.txt
fi
sleep 60
done
This is the noscript. It runs every 1 minute. I was planning to use setsid ./noscript.sh but I didn't find a way to exit that process easily. So, I am not doing that. Should I run it as a cronjob(It doesn't make much sense to run this as a cronjob though. as it's already sleeping for 60 seconds. Anything you can think of?)
https://redd.it/1afjbg7
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Running a command inside another command in a one liner?
Im not too familiar with bash so i might not be using the correct terms. What im trying to do is make a one liner that makes a PUT request to a page with its body being the output of a command.
Im trying to make this
date -Iseconds | head -c -7
go in the "value" of this command
curl -X PUT -H "Content-Type: application/json" -d '{"UTC":"value"}' address
and idea is ill run this with crontab every minute or so to update the time of a "smart" appliance (philips hue bridge)
https://redd.it/1afmquq
@r_bash
Im not too familiar with bash so i might not be using the correct terms. What im trying to do is make a one liner that makes a PUT request to a page with its body being the output of a command.
Im trying to make this
date -Iseconds | head -c -7
go in the "value" of this command
curl -X PUT -H "Content-Type: application/json" -d '{"UTC":"value"}' address
and idea is ill run this with crontab every minute or so to update the time of a "smart" appliance (philips hue bridge)
https://redd.it/1afmquq
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Need to create a loop that make a mkdir -p with the openssl rand -hex 2 output.
Hi guys.
Need to create a loop that make a mkdir -p with the openssl rand -hex 2 output.
I try this
\#!/bin/bash
var=$(openssl rand -hex 2)
for line in $var
do
mkdir "${line}"
done
But interact once and then stop!
I need to fullfill every possible combination which openssl rand -hex 2 gives.
Short history: need create hex directory from 0000 to FFFF
​
Thanks
https://redd.it/1afrne9
@r_bash
Hi guys.
Need to create a loop that make a mkdir -p with the openssl rand -hex 2 output.
I try this
\#!/bin/bash
var=$(openssl rand -hex 2)
for line in $var
do
mkdir "${line}"
done
But interact once and then stop!
I need to fullfill every possible combination which openssl rand -hex 2 gives.
Short history: need create hex directory from 0000 to FFFF
​
Thanks
https://redd.it/1afrne9
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
why is this noscript running every second(even multiple times per second)?
host=www.google.com
port=443
while true; do
current_time=$(date +%H:%M:%S)
r=$(bash -c 'exec 3<> /dev/tcp/'$host'/'$port';echo $?' 2>/dev/null)
if [ "$r" = "0" ]; then
echo "[$current_time] $host $port is open" >> log_file.txt
else
echo "[$current_time] $host $port is closed" >> log_file.txt
fi
done
I put it into a cronjob every 1 hour or whatever, it'll always run every 1 second and sometimes multiple times per second.
https://redd.it/1ag2sg9
@r_bash
host=www.google.com
port=443
while true; do
current_time=$(date +%H:%M:%S)
r=$(bash -c 'exec 3<> /dev/tcp/'$host'/'$port';echo $?' 2>/dev/null)
if [ "$r" = "0" ]; then
echo "[$current_time] $host $port is open" >> log_file.txt
else
echo "[$current_time] $host $port is closed" >> log_file.txt
fi
done
I put it into a cronjob every 1 hour or whatever, it'll always run every 1 second and sometimes multiple times per second.
https://redd.it/1ag2sg9
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Is it possible to get the exit code of mv in "mv $folder $target &"
Is it possible to get the exit code of the mv command on the 2nd last line without messing up the progress bar function?
#!/usr/bin/env bash
# Shell Colors
Red='\e0;31m' # ${Red}
Yellow='\e[0;33m' # ${Yellow}
Cyan='\e[0;36m' # ${Cyan}
Error='\e[41m' # ${Error}
Off='\e[0m' # ${Off}
progbar(){
# $1 is pid of process
# $2 is string to echo
local PROC
local delay
local dots
local progress
PROC="$1"
delay="0.3"
dots=""
while [[ -d /proc/$PROC ]; do
dots="${dots}."
progress="$dots"
if [ ${#dots} -gt "10" ]; then
dots=""
progress=" "
fi
echo -ne " ${2}$progress\r"; sleep "$delay"
done
echo -e "$2 "
return 0
}
action="Moving"
sourcevol="volume1"
targetvol="/volume2"
folder="@foobar"
mv -f "/${sourcevol}/$folder" "${targetvol}" &
progbar $! "mv ${action} /${sourcevol}/$folder to ${Cyan}$targetvol${Off}"
https://redd.it/1ag3qiz
@r_bash
Is it possible to get the exit code of the mv command on the 2nd last line without messing up the progress bar function?
#!/usr/bin/env bash
# Shell Colors
Red='\e0;31m' # ${Red}
Yellow='\e[0;33m' # ${Yellow}
Cyan='\e[0;36m' # ${Cyan}
Error='\e[41m' # ${Error}
Off='\e[0m' # ${Off}
progbar(){
# $1 is pid of process
# $2 is string to echo
local PROC
local delay
local dots
local progress
PROC="$1"
delay="0.3"
dots=""
while [[ -d /proc/$PROC ]; do
dots="${dots}."
progress="$dots"
if [ ${#dots} -gt "10" ]; then
dots=""
progress=" "
fi
echo -ne " ${2}$progress\r"; sleep "$delay"
done
echo -e "$2 "
return 0
}
action="Moving"
sourcevol="volume1"
targetvol="/volume2"
folder="@foobar"
mv -f "/${sourcevol}/$folder" "${targetvol}" &
progbar $! "mv ${action} /${sourcevol}/$folder to ${Cyan}$targetvol${Off}"
https://redd.it/1ag3qiz
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Running commands written in file
I am not familiar with advanced bash commands at all. I know the decent way for asking for help would be having at least a half-baked solution, but i have no idea how to do this. I would like to easily run prehook commands before testing for each project, and for that I would like to create a bashrc (zshrc) alias that searches the /test.config file for the commands that are in it, and runs them in sequence.
The format in the file is
{pre_hooks, [
{ct, "command1"},
{ct, "command2"}
]}.
The number and content of the commands is different for each project, and the the file contains a lot of thigs besides this.
I have tried to search it but could not find a relevant result. I will of course check what any given solution does, i want to learn this stuff. Thanks for the help!
https://redd.it/1aga2vn
@r_bash
I am not familiar with advanced bash commands at all. I know the decent way for asking for help would be having at least a half-baked solution, but i have no idea how to do this. I would like to easily run prehook commands before testing for each project, and for that I would like to create a bashrc (zshrc) alias that searches the /test.config file for the commands that are in it, and runs them in sequence.
The format in the file is
{pre_hooks, [
{ct, "command1"},
{ct, "command2"}
]}.
The number and content of the commands is different for each project, and the the file contains a lot of thigs besides this.
I have tried to search it but could not find a relevant result. I will of course check what any given solution does, i want to learn this stuff. Thanks for the help!
https://redd.it/1aga2vn
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Variable not global
I have the following code in my noscript and I can't figure out why
printreleasenotes() {
mapfile -t pkgs < <(comm -12 <( sort "$conf" | cut -d' ' -f 1) <( awk '{ sub("^#.| #.", "") } !NF { next } { print $1 }' "$cache" | sort))
if ((${#pkgs@})); then
local url
printf "\n%s\n" "# Release notes:"
for package in "${pkgs@}"; do
while read -r line; do
pkgswithlink="${line%% }"
if [[ "$package" == "$pkgs_with_link" ]]; then
url="${line## }"
printf "%s\n" "# $(tput setaf 1)$pkgswithlink$(tput sgr0): $url"
pkgswithlinks+=("$url")
break
fi
done < "$conf"
done
printf "%s" "all my links:" "${pkgswithlinks@}"
fi
}
Quick google search shows piping involves a subshell and that variables define inside will not be accessible globally. But the while loop does not involves any pipes.
Any ideas and the recommended way to make it accessible globally? Also is there any point in using
https://redd.it/1aglq82
@r_bash
I have the following code in my noscript and I can't figure out why
pkgs_with_links (not pkg_with_link, which is local) is not accessible globally:printreleasenotes() {
mapfile -t pkgs < <(comm -12 <( sort "$conf" | cut -d' ' -f 1) <( awk '{ sub("^#.| #.", "") } !NF { next } { print $1 }' "$cache" | sort))
if ((${#pkgs@})); then
local url
printf "\n%s\n" "# Release notes:"
for package in "${pkgs@}"; do
while read -r line; do
pkgswithlink="${line%% }"
if [[ "$package" == "$pkgs_with_link" ]]; then
url="${line## }"
printf "%s\n" "# $(tput setaf 1)$pkgswithlink$(tput sgr0): $url"
pkgswithlinks+=("$url")
break
fi
done < "$conf"
done
printf "%s" "all my links:" "${pkgswithlinks@}"
fi
}
Quick google search shows piping involves a subshell and that variables define inside will not be accessible globally. But the while loop does not involves any pipes.
Any ideas and the recommended way to make it accessible globally? Also is there any point in using
declare to initialize a variable? Would it be a good idea to initialize all variables intended to be used globally at the beginning of the noscript so that for maintaining the noscript in the future it's easier to see all the global variables and not accidentally add in code involving a new variable that might be named the same as the global variable?https://redd.it/1aglq82
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
can you make a text game in bash?
i just randomly started learning bash from youtube 4 fun although it'd be useful too for what i am doing and my job in the future, and now i have a question, can you make a decent text game in bash? i'd be quite fun to do so
https://redd.it/1agnvoh
@r_bash
i just randomly started learning bash from youtube 4 fun although it'd be useful too for what i am doing and my job in the future, and now i have a question, can you make a decent text game in bash? i'd be quite fun to do so
https://redd.it/1agnvoh
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Alias for Executable in Makefile
How do I make an executable an alias? So if I had an executable called ./out, and I wanted to run call "out2" to run the executable instead, how'd I do it?
I want to know because I want to run an executable file without having to use a command that starts with "./"
https://redd.it/1agpwug
@r_bash
How do I make an executable an alias? So if I had an executable called ./out, and I wanted to run call "out2" to run the executable instead, how'd I do it?
I want to know because I want to run an executable file without having to use a command that starts with "./"
https://redd.it/1agpwug
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Dynamic FFmpeg build noscript with extra codecs activated
This noscript represents the result of significant effort to compile FFmpeg while incorporating numerous additional programs into its code all done in Bash.
When you execute the noscript multiple times, it will automatically track the current version of each program. If a newer version is detected, it will update them accordingly. This ensures that you consistently have access to the latest code when building and running FFmpeg.
The noscript will detect if you have Nvidia GPU's and ask if you want to install the CUDA SDK toolkit. It will also detect AMD and install header files for it's GPU.
​
Supported Codecs
GitHub Script
https://redd.it/1agri1v
@r_bash
This noscript represents the result of significant effort to compile FFmpeg while incorporating numerous additional programs into its code all done in Bash.
When you execute the noscript multiple times, it will automatically track the current version of each program. If a newer version is detected, it will update them accordingly. This ensures that you consistently have access to the latest code when building and running FFmpeg.
The noscript will detect if you have Nvidia GPU's and ask if you want to install the CUDA SDK toolkit. It will also detect AMD and install header files for it's GPU.
​
Supported Codecs
GitHub Script
https://redd.it/1agri1v
@r_bash
Why does this math expression throw an error and how can I catch it?
The purpose is to convert a string to +ve int. It works except for if the string begins with a number.
echo "$(( "9" ))"
>9
echo "$(( "99" ))"
> 99
echo "$(( "word" ))"
> 0
echo "$(( "word9" ))"
> 0
echo "$(( "9word" ))"
> bash: 9word: value too great for base (error token is "9word")
echo "$(( "99word" ))"
> bash: 99word: value too great for base (error token is "99word")
I don't much care if "9word" fails but I'd like for it to evaluate to 0.
https://redd.it/1ahqmmw
@r_bash
The purpose is to convert a string to +ve int. It works except for if the string begins with a number.
echo "$(( "9" ))"
>9
echo "$(( "99" ))"
> 99
echo "$(( "word" ))"
> 0
echo "$(( "word9" ))"
> 0
echo "$(( "9word" ))"
> bash: 9word: value too great for base (error token is "9word")
echo "$(( "99word" ))"
> bash: 99word: value too great for base (error token is "99word")
I don't much care if "9word" fails but I'd like for it to evaluate to 0.
https://redd.it/1ahqmmw
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community