best way to read settings from a file?
One of the noscripts I am making, I am going to put the settings in a seperate file that the user can modify. But what is the best way to get the settings into the main noscript: Grep? Awk and Line numbers?
https://redd.it/13su4kp
@r_bash
One of the noscripts I am making, I am going to put the settings in a seperate file that the user can modify. But what is the best way to get the settings into the main noscript: Grep? Awk and Line numbers?
https://redd.it/13su4kp
@r_bash
Reddit
r/bash on Reddit: best way to read settings from a file?
Posted by u/Aeul289 - No votes and no comments
exec "$0" "$@" causing getopt invalid option
I have noscripts that update themselves to the latest version from github. After they update I want them to run the updated noscript, but `exec "$0" "$@"` results in the noscript having getopt errors.
If I run the noscript with "`noscript.sh``-snfr`" it runs as it should.
If I run the noscript with "`noscript.sh``-s -n -f -r`" it causes a getopt error.
In bash 4.3.43 the error is:
getopt: unrecognized option '- -n -f -r'
In bash 4.4.23 the error is:
getopt: invalid option -- ' '
getopt: invalid option -- '-'
getopt: invalid option -- ' '
getopt: invalid option -- '-'
getopt: invalid option -- ' '
getopt: invalid option -- '-'
Here's the part of the noscript causing the issue:
#!/usr/bin/env bash
usage(){
cat <<EOF
Usage: $(basename "$0") [options]
Options:
-s, --showedits Show edits made to <model>_host db and db.new file(s)
-n, --noupdate Prevent DSM updating the compatible drive databases
-m, --memory Disable memory compatibility checking
-f, --force Force DSM to not check drive compatibility
--restore Undo all changes made by the noscript
-a, --autoupdate Auto update noscript (useful when noscript is scheduled)
-h, --help Show this help message
-v, --version Show the noscript version
EOF
exit 0
}
# Save options used
#args="$*"
args=("$*")
# Check for flags with getopt
if options="$(getopt -o abcdefghijklmnopqrstuvwxyz0123456789 -a -l \
restore,showedits,noupdate,nodbupdate,memory,force,help,version \
-- "$@")"; then
eval set -- "$options"
while true; do
case "${1,,}" in
--restore) # Restore changes from backups
restore=yes
break
;;
-s|--showedits) # Show edits done to host db file
showedits=yes
;;
-n|--nodbupdate|--noupdate) # Disable disk compatibility db updates
nodbupdate=yes
;;
-m|--memory) # Disable "support_memory_compatibility"
ram=yes
;;
-f|--force) # Disable "support_disk_compatibility"
force=yes
;;
-h|--help) # Show usage options
usage
;;
-v|--version) # Show noscript version
noscriptversion
;;
--)
shift
break
;;
*) # Show usage options
echo -e "Invalid option '$1'\n"
usage "$1"
;;
esac
shift
done
else
echo
usage
fi
# Show options used
#echo "Using options: $args"
echo "Using options: ${args[@]}"
# Copy new noscript over current noscript
echo "Reload noscript? [y/n]"
read -r reply
if [[ ${reply,,} == "y" ]]; then
echo -e "------------------------------------------------------------\n"
#echo "debug: exec" "$0" "$@" # debug
#echo "debug: exec" "$0" "$args" # debug
echo "debug: exec" "$0" "${args[@]}" # debug
#exec "$0" "$@"
#exec "$0" "$args"
exec "$0" "${args[@]}"
fi
https://redd.it/13t2jcc
@r_bash
I have noscripts that update themselves to the latest version from github. After they update I want them to run the updated noscript, but `exec "$0" "$@"` results in the noscript having getopt errors.
If I run the noscript with "`noscript.sh``-snfr`" it runs as it should.
If I run the noscript with "`noscript.sh``-s -n -f -r`" it causes a getopt error.
In bash 4.3.43 the error is:
getopt: unrecognized option '- -n -f -r'
In bash 4.4.23 the error is:
getopt: invalid option -- ' '
getopt: invalid option -- '-'
getopt: invalid option -- ' '
getopt: invalid option -- '-'
getopt: invalid option -- ' '
getopt: invalid option -- '-'
Here's the part of the noscript causing the issue:
#!/usr/bin/env bash
usage(){
cat <<EOF
Usage: $(basename "$0") [options]
Options:
-s, --showedits Show edits made to <model>_host db and db.new file(s)
-n, --noupdate Prevent DSM updating the compatible drive databases
-m, --memory Disable memory compatibility checking
-f, --force Force DSM to not check drive compatibility
--restore Undo all changes made by the noscript
-a, --autoupdate Auto update noscript (useful when noscript is scheduled)
-h, --help Show this help message
-v, --version Show the noscript version
EOF
exit 0
}
# Save options used
#args="$*"
args=("$*")
# Check for flags with getopt
if options="$(getopt -o abcdefghijklmnopqrstuvwxyz0123456789 -a -l \
restore,showedits,noupdate,nodbupdate,memory,force,help,version \
-- "$@")"; then
eval set -- "$options"
while true; do
case "${1,,}" in
--restore) # Restore changes from backups
restore=yes
break
;;
-s|--showedits) # Show edits done to host db file
showedits=yes
;;
-n|--nodbupdate|--noupdate) # Disable disk compatibility db updates
nodbupdate=yes
;;
-m|--memory) # Disable "support_memory_compatibility"
ram=yes
;;
-f|--force) # Disable "support_disk_compatibility"
force=yes
;;
-h|--help) # Show usage options
usage
;;
-v|--version) # Show noscript version
noscriptversion
;;
--)
shift
break
;;
*) # Show usage options
echo -e "Invalid option '$1'\n"
usage "$1"
;;
esac
shift
done
else
echo
usage
fi
# Show options used
#echo "Using options: $args"
echo "Using options: ${args[@]}"
# Copy new noscript over current noscript
echo "Reload noscript? [y/n]"
read -r reply
if [[ ${reply,,} == "y" ]]; then
echo -e "------------------------------------------------------------\n"
#echo "debug: exec" "$0" "$@" # debug
#echo "debug: exec" "$0" "$args" # debug
echo "debug: exec" "$0" "${args[@]}" # debug
#exec "$0" "$@"
#exec "$0" "$args"
exec "$0" "${args[@]}"
fi
https://redd.it/13t2jcc
@r_bash
Reddit
r/bash on Reddit: exec "$0" "$@" causing getopt invalid option
Posted by u/DaveR007 - No votes and no comments
find, filenames with leading "-", but cannot use "--"
I have a wrapper noscript around
So my question, what should I do? The idea is, if filenames start with a dash, then I can safely add
files="$(find "${symlinks}" \
-O3 \
"${@}" "${stdin@}" \
-readable \
-nowarn \
-maxdepth "${optmaxdepth}" \
${xdev} \
${opttype} \
${executabletype} \
-name "${allpattern}" \
"${filtermode}" "${filterpattern}" \
-regextype posix-extended \
"${extendedmode}" "${extendedpattern}" \
-print \
2>/dev/null)"
About the unquoted options, I know that is usually not very safe to do. But these options are controlled and cannot be anything else than correct or empty (in theory). My focus is on
If adding
https://redd.it/13t9dmd
@r_bash
I have a wrapper noscript around
find (and a few other) command. The noscript itself is using Bash's getopts and double dash -- to stop parsing options works as intended. However, there is a problem when giving the arguments over to find command. If a file is a relative path and starts directly with a dash such as -New File, then find command will fail. All other tools and the noscript are handling this correctly. My problem is, I can't use -- with find, because options need to appear after the filenames.So my question, what should I do? The idea is, if filenames start with a dash, then I can safely add
./ in front of them. For anyone who wants to have a look at the code (over 500 lines of code): https://github.com/thingsiplay/findpick/blob/main/fp and here is how I run find at the moment:files="$(find "${symlinks}" \
-O3 \
"${@}" "${stdin@}" \
-readable \
-nowarn \
-maxdepth "${optmaxdepth}" \
${xdev} \
${opttype} \
${executabletype} \
-name "${allpattern}" \
"${filtermode}" "${filterpattern}" \
-regextype posix-extended \
"${extendedmode}" "${extendedpattern}" \
-print \
2>/dev/null)"
About the unquoted options, I know that is usually not very safe to do. But these options are controlled and cannot be anything else than correct or empty (in theory). My focus is on
"${@}" "${stdin[@]}" \ . If adding
./ is my only option (the only one I can think of at the moment), how would I do that efficiently for both, positional arguments list and stdin array?https://redd.it/13t9dmd
@r_bash
GitHub
findpick/fp at main · thingsiplay/findpick
General purpose file picker combining "find" command with a fuzzy finder. - findpick/fp at main · thingsiplay/findpick
Opinions about Bash customization project
Hello all,
I would love to hear some of your opinions about my OpenSource project that allow you to enhance your bash experience. It's called HomeSetup and can be installed or tried in a docker container.
https://github.com/yorevs/homesetup
Appreciated any of your help
https://redd.it/13tcs1f
@r_bash
Hello all,
I would love to hear some of your opinions about my OpenSource project that allow you to enhance your bash experience. It's called HomeSetup and can be installed or tried in a docker container.
https://github.com/yorevs/homesetup
Appreciated any of your help
https://redd.it/13tcs1f
@r_bash
GitHub
GitHub - yorevs/homesetup: The ultimate Terminal experience!
The ultimate Terminal experience! Contribute to yorevs/homesetup development by creating an account on GitHub.
Should i order an Index array with a side of hash?
I have a program i wish to run on my system every so often. It will query some websites with some flags, etc...
My thought is to have an indexed array, where each entry would hold several values, such as:
-Name
-url
-flags
-destination
Is this the best approach using bash or is there a better way to implement this? At the moment, my plan is to have the structure fully initiated within the noscript and not dynamically changed during execution. (I haven't used bash in years, hence my question.)
Thank you for your time,
Q
https://redd.it/13td3kg
@r_bash
I have a program i wish to run on my system every so often. It will query some websites with some flags, etc...
My thought is to have an indexed array, where each entry would hold several values, such as:
-Name
-url
-flags
-destination
Is this the best approach using bash or is there a better way to implement this? At the moment, my plan is to have the structure fully initiated within the noscript and not dynamically changed during execution. (I haven't used bash in years, hence my question.)
Thank you for your time,
Q
https://redd.it/13td3kg
@r_bash
Reddit
r/bash on Reddit: Should i order an Index array with a side of hash?
Posted by u/PandaEquivalent - No votes and 1 comment
How to include a horizontal line after a command?
E.g.:
$ echo 'Hello world'
Hello world
──────────────────────────────────────
https://redd.it/13u9fnj
@r_bash
E.g.:
$ echo 'Hello world'
Hello world
──────────────────────────────────────
https://redd.it/13u9fnj
@r_bash
Reddit
r/bash on Reddit: How to include a horizontal line after a command?
Posted by u/AlbertoAru - No votes and 3 comments
Modifying sshto (dialog) with host flags?
Hi. I've been using the sshto noscript/dialog for some time now and really enjoy using it.
​
I also use Kitty browser, and tend to use a "kitten" when ssh'ing to production machines so that my terminal's color scheme changes. This way I can quickly differentiate between production/mission-critical machines and ones that aren't so I'm less likely to make a stupid mistake on a production host.
​
I have been able to do this so far by putting all these hosts under a single group and then I modified sshto's go_to_target function to launch a kitten when the target is a member of that group. It only works if I have the view filtered by that group, though, and it also prevents me from categorizing hosts like I'd like.
​
I had the idea to add a tag to these devices in the ssh conf files, probably something like PROD# after the host denoscription (ex.
​
I've tried adding the tag and then stripping it out of the displayed denoscription by way of regex, but it seems that the
​
I posted in the sshto github as well, but in my tinkering and research I've seen several discussions around sshto and thought somebody may have some ideas here and I wouldn't have to bug the developer with a feature that I'm possibly the only person interested in. (Github post)
https://redd.it/13uhywm
@r_bash
Hi. I've been using the sshto noscript/dialog for some time now and really enjoy using it.
​
I also use Kitty browser, and tend to use a "kitten" when ssh'ing to production machines so that my terminal's color scheme changes. This way I can quickly differentiate between production/mission-critical machines and ones that aren't so I'm less likely to make a stupid mistake on a production host.
​
I have been able to do this so far by putting all these hosts under a single group and then I modified sshto's go_to_target function to launch a kitten when the target is a member of that group. It only works if I have the view filtered by that group, though, and it also prevents me from categorizing hosts like I'd like.
​
I had the idea to add a tag to these devices in the ssh conf files, probably something like PROD# after the host denoscription (ex.
Host prod-serv1 #Primary Server#PROD#). But, picky jerk that I am, I don't want to see this tag in the dialog list. Or if I have to see the tag I'd like to move it to it's own column. IDK how to do this.​
I've tried adding the tag and then stripping it out of the displayed denoscription by way of regex, but it seems that the
go_to_target function is actually using strictly what's visible in the dialog, so if it's not visible I can't use it to choose behavior. So then I though about adding a third column, something like a one-character flag (ex. Host prod-serv1 #Primary Server#P#), but I can't figure out how to modify the dialog to add this extra column. Every way I've tried either makes no difference or shows the additional data but still only displays two columns so all the data gets out of line. Can anybody provide any suggestions on how I could achieve either of these goals?​
I posted in the sshto github as well, but in my tinkering and research I've seen several discussions around sshto and thought somebody may have some ideas here and I wouldn't have to bug the developer with a feature that I'm possibly the only person interested in. (Github post)
https://redd.it/13uhywm
@r_bash
GitHub
GitHub - vaniacer/sshto: Small bash noscript to manage your ssh connections. It builds menu (via dialog) from your ~/.ssh/config.…
Small bash noscript to manage your ssh connections. It builds menu (via dialog) from your ~/.ssh/config. It can not only connect but also to run commands, copy files, tunnel ports. - vaniacer/sshto
Escape value \x1b echoing problem
Having escape value echoed, it removes subsequent echo's alphanumeric character. Here's the code:
#/bin/bash
#
echo "12345"
echo $'\x1b'
echo "12345"
echo "12345
Script output:
12345
[empty line\]
2345 <--- lost the first character
12345
Evidently there is some built in magic for escape value, but what can it be? Is there an idiomatic way to "flush" the effect of '\\x1b' in terminal stream (did try sed, stty, printf etc., but no solution)?
https://redd.it/13uob2m
@r_bash
Having escape value echoed, it removes subsequent echo's alphanumeric character. Here's the code:
#/bin/bash
#
echo "12345"
echo $'\x1b'
echo "12345"
echo "12345
Script output:
12345
[empty line\]
2345 <--- lost the first character
12345
Evidently there is some built in magic for escape value, but what can it be? Is there an idiomatic way to "flush" the effect of '\\x1b' in terminal stream (did try sed, stty, printf etc., but no solution)?
https://redd.it/13uob2m
@r_bash
Reddit
r/bash on Reddit: Escape value \x1b echoing problem
Posted by u/Oskar_no_number - No votes and 1 comment
Dynamic build of a command pipe which can handle options
https://gist.github.com/nkh/718f897cef0bacc0b75da6254456e889
The gist contains and explanation of how to do it with multiple examples, you can run the gist to see the results. This was written while working on https://github.com/nkh/ftl, which is a file manager written in bash.
Some previous discussions:
https://stackoverflow.com/questions/52538881/dynamically-building-a-command-pipe-in-bash
https://stackoverflow.com/questions/63023975/can-i-make-a-shell-function-in-as-a-pipeline-conditionally-disappear-without
https://redd.it/13uv87b
@r_bash
https://gist.github.com/nkh/718f897cef0bacc0b75da6254456e889
The gist contains and explanation of how to do it with multiple examples, you can run the gist to see the results. This was written while working on https://github.com/nkh/ftl, which is a file manager written in bash.
Some previous discussions:
https://stackoverflow.com/questions/52538881/dynamically-building-a-command-pipe-in-bash
https://stackoverflow.com/questions/63023975/can-i-make-a-shell-function-in-as-a-pipeline-conditionally-disappear-without
https://redd.it/13uv87b
@r_bash
Gist
how to dynamically create a pipeline that handles options
how to dynamically create a pipeline that handles options - option_pipe
what does a -z option in a bare if statement do?
Hi.
Am new to bash. I'm looking over a project and I'm seeing a -z option. I can't find any info on this online,
it's something like this:
if [ -z "$APP_CONFIG" ! -z "$CONFIG_DIR" ]
then ...
What is this exactly doing?
Cheers
https://redd.it/13v3dv8
@r_bash
Hi.
Am new to bash. I'm looking over a project and I'm seeing a -z option. I can't find any info on this online,
it's something like this:
if [ -z "$APP_CONFIG" ! -z "$CONFIG_DIR" ]
then ...
What is this exactly doing?
Cheers
https://redd.it/13v3dv8
@r_bash
how to make read command send highlighted text message
while true; do
read -p "Enter an argument : " arg
trans "$arg"
done
https://redd.it/13v32aq
@r_bash
while true; do
read -p "Enter an argument : " arg
trans "$arg"
done
https://redd.it/13v32aq
@r_bash
Reddit
r/bash on Reddit: how to make read command send highlighted text message
Posted by u/mapping1123 - No votes and 1 comment
Integrating Bash noscripts with Publish-Subscribe Messaging
https://m10k.eu/2023/05/27/toolbox-pubsub.html
https://redd.it/13vmggc
@r_bash
https://m10k.eu/2023/05/27/toolbox-pubsub.html
https://redd.it/13vmggc
@r_bash
Question regarding shell
First of all, I am sorry for asking a silly question.
echo "/usr/bin/bash" > myls
Now when i run ./myls (after setting permission) it should open my shell right but i am not getting any output, why is that happening?
https://redd.it/13vnhym
@r_bash
First of all, I am sorry for asking a silly question.
echo "/usr/bin/bash" > myls
Now when i run ./myls (after setting permission) it should open my shell right but i am not getting any output, why is that happening?
https://redd.it/13vnhym
@r_bash
Reddit
r/bash on Reddit: Question regarding shell
Posted by u/Alternative_Brick_72 - No votes and 9 comments
Looking up for some projects ideas in BASH
I don't know where to look but I am stuck finding some good projects for Bash Scripting, if you have any suggestions then help.
https://redd.it/13vw3oh
@r_bash
I don't know where to look but I am stuck finding some good projects for Bash Scripting, if you have any suggestions then help.
https://redd.it/13vw3oh
@r_bash
Reddit
r/bash on Reddit: Looking up for some projects ideas in BASH
Posted by u/broken_py - No votes and no comments
how to make graphical lines in a tui noscript?
​
https://preview.redd.it/rbwtz17ad23b1.png?width=81&format=png&auto=webp&v=enabled&s=178e21ff6f99409f03953b86634d2d329c2319be
I'm trying to make a tui program and I want straight lines like these, (for example ncmpcpp and bottom have them) But I have no idea how to make them using shell noscript. Are they just underscores or are they actually a line? and how are they positioned?
https://redd.it/13w06cc
@r_bash
​
https://preview.redd.it/rbwtz17ad23b1.png?width=81&format=png&auto=webp&v=enabled&s=178e21ff6f99409f03953b86634d2d329c2319be
I'm trying to make a tui program and I want straight lines like these, (for example ncmpcpp and bottom have them) But I have no idea how to make them using shell noscript. Are they just underscores or are they actually a line? and how are they positioned?
https://redd.it/13w06cc
@r_bash
Question about "ls" error messages, when/why are they suppressed if redirecting the output?
Some background for context only: I am creating a simple bash noscript that will check for stale file handles (most likely Samba mounts that became invalid because the remote system was rebooted, or similar), that I will run from
​
The problem is that I wanted to find stale file handles by running
​
In this case, my workaround could be to list all files in the directory, pipe the output into a
(It's a bit annoying have a solution that works at the bash command line, but does not work in a noscript.)
https://redd.it/13wg9ol
@r_bash
Some background for context only: I am creating a simple bash noscript that will check for stale file handles (most likely Samba mounts that became invalid because the remote system was rebooted, or similar), that I will run from
/etc/crontab. The noscript will then umount/mount to fix it, and this is not the part that I have a problem with.​
The problem is that I wanted to find stale file handles by running
ls -1 and looking for an error message such as ls: cannot access 'BAD_MOUNTPOINT': Stale file handle. However, when I try to redirect the output to a mktemp file or pipe it to grep or similar, this error message does not seem to be displayed. Neither on stdout or stderr. It seems to me that ls prints different things if it detects that a console (or not) will receive stdout.​
In this case, my workaround could be to list all files in the directory, pipe the output into a
while IFS= read -r loop and use stat on each filename, which still works from a noscript. But I'd like to know why the ls error output is suppressed. Any ideas? (It's a bit annoying have a solution that works at the bash command line, but does not work in a noscript.)
https://redd.it/13wg9ol
@r_bash
Reddit
r/bash on Reddit: Question about "ls" error messages, when/why are they suppressed if redirecting the output?
Posted by u/DuDuSmitsenmadu - No votes and 2 comments
Problem with * in bash
Hello everyone,
I currently having a problem where the "*" is not replaced by anything in my bash file.
To add context, I'm trying to organize a game collection where I put every game that starts with a "t" in a "t" folder.
To avoid repeating the same two line in my she'll again and again, I wrote a bash noscript to do it for me.
I give it an argument (the letter "t" for example) and it create a folder and move every file beginning with t in it.
I wrote :
#!/bin/bash
mkdir $1
mv ${1}*.a26 $1/
But when I try to execute it, the mv command fail saying that it's impossible to evaluate 't*.a26' because there's no file or folder with this name.
(Just to be clear, I have several files beginning with t).
I can see that the problem comes because of the "*" that is not replaced with anything but I searched and didn't find a solution, neither by myself neither by researching.
So if someone can help me, I will be very thankful
https://redd.it/13wn10e
@r_bash
Hello everyone,
I currently having a problem where the "*" is not replaced by anything in my bash file.
To add context, I'm trying to organize a game collection where I put every game that starts with a "t" in a "t" folder.
To avoid repeating the same two line in my she'll again and again, I wrote a bash noscript to do it for me.
I give it an argument (the letter "t" for example) and it create a folder and move every file beginning with t in it.
I wrote :
#!/bin/bash
mkdir $1
mv ${1}*.a26 $1/
But when I try to execute it, the mv command fail saying that it's impossible to evaluate 't*.a26' because there's no file or folder with this name.
(Just to be clear, I have several files beginning with t).
I can see that the problem comes because of the "*" that is not replaced with anything but I searched and didn't find a solution, neither by myself neither by researching.
So if someone can help me, I will be very thankful
https://redd.it/13wn10e
@r_bash
Reddit
r/bash on Reddit: Problem with * in bash
Posted by u/ikkonikk - No votes and 2 comments
Sshto fix
Hi, I've just fixed some dumb error in sshto it didn't work if \~/.ssh/config file was missing O_o
Fixed that, enjoy)
https://redd.it/13wqfjs
@r_bash
Hi, I've just fixed some dumb error in sshto it didn't work if \~/.ssh/config file was missing O_o
Fixed that, enjoy)
https://redd.it/13wqfjs
@r_bash
GitHub
GitHub - vaniacer/sshto: Small bash noscript to manage your ssh connections. It builds menu (via dialog) from your ~/.ssh/config.…
Small bash noscript to manage your ssh connections. It builds menu (via dialog) from your ~/.ssh/config. It can not only connect but also to run commands, copy files, tunnel ports. - vaniacer/sshto
declaring array using a variable
Given that I can declare an array like this and it creates a three element array perfectly ...
$ declare -a myarray=( 0=apple 1=pear 2=banana )
$ echo ${myarray1}
pear
...then why does this not behave in exactly the same way?
$ arraystring="0=apple 1=pear 2=banana"
$ declare -a myarray=( $arraystring )
$ echo ${myarray1}
1=pear
It still creates a three element array, but includes the key "1=" as part of the element contents. Why is this? I'm sure it has something to do with quoting but can't work it out. Quoting all the elements or quoting $arraystring doesn't seem to make a difference here.
https://redd.it/13wspgg
@r_bash
Given that I can declare an array like this and it creates a three element array perfectly ...
$ declare -a myarray=( 0=apple 1=pear 2=banana )
$ echo ${myarray1}
pear
...then why does this not behave in exactly the same way?
$ arraystring="0=apple 1=pear 2=banana"
$ declare -a myarray=( $arraystring )
$ echo ${myarray1}
1=pear
It still creates a three element array, but includes the key "1=" as part of the element contents. Why is this? I'm sure it has something to do with quoting but can't work it out. Quoting all the elements or quoting $arraystring doesn't seem to make a difference here.
https://redd.it/13wspgg
@r_bash
Reddit
r/bash on Reddit: declaring array using a variable
Posted by u/asquartz - No votes and 1 comment