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
This media is not supported in your browser
VIEW IN TELEGRAM
I made a simple noscript for splitting a physical monitor into virtual ones
https://redd.it/13wvk4a
@r_bash
https://redd.it/13wvk4a
@r_bash
How to compress FileB with respect to, but not compressing, FileA?
Lets say I have FileA and FileB. They are nearly identical files. I am looking to compress the redundant data in FileB with respect to the original data in FileA, then go on to embed the compressed FileB within a metadata field of FileA for export. Eventually would need a process to reverse this as well.
I am not looking for any code but if this is possible and hopefully a point in the right direction. Thanks!
https://redd.it/13wwxtq
@r_bash
Lets say I have FileA and FileB. They are nearly identical files. I am looking to compress the redundant data in FileB with respect to the original data in FileA, then go on to embed the compressed FileB within a metadata field of FileA for export. Eventually would need a process to reverse this as well.
I am not looking for any code but if this is possible and hopefully a point in the right direction. Thanks!
https://redd.it/13wwxtq
@r_bash
Reddit
r/bash on Reddit: How to compress File_B with respect to, but not compressing, File_A?
Posted by u/rM-self-serve - No votes and 3 comments
Is this regex correct for what I'm trying to do?
So, first-off, I will admit, I know little about regex patterns. I'm trying to get a noscript to identify a \[music\] track filename with a track number, and only if the track number is a single digit, add a leading zero. I'm gonna get some cringes from the audience here, but please be gentle; I'm still very much a beginner, this is how the code for that looks at the moment, and spoiler alert, though no surprise to some, it doesn't work:
[if..then block begins]
elif [[ $filename =~ ^[0-9]+\ .+\.mp3$ ]]; then
local track_number=$(echo "$filename" | cut -d' ' -f1)
local noscript=$(echo "$filename" | cut -d' ' -f2-)
# Add leading zero if track number is a single character
if [[ ${#track_number} -eq 1 ]]; then
track_number="0$track_number"
fi
local new_filename="${track_number} ${noscript}"
if [[ "$filename" != "$new_filename" ]]; then
mv "$mp3_file" "$sub_dir/$new_filename"
log "Renamed: $filename --> $new_filename"
((files_renamed++))
else
log "No changes needed for: $filename"
fi
[if..then block continues]
I have plenty of tracks that are named like "1 Kryptonite.mp3" which I want renamed to "01 Kryptonite.mp3". I have close to 10,000 tracks in my library, so I'm not doing this by hand for the first 9 tracks of 677 albums.
Where am I going wrong, style aside? (I'm literally just trying to get this to work before I make it pretty)
And yes, everything is correctly declared and everything; this is the only part of the noscript I'm having issue with; the rest of the renaming conditions work fine.
https://redd.it/13wviwz
@r_bash
So, first-off, I will admit, I know little about regex patterns. I'm trying to get a noscript to identify a \[music\] track filename with a track number, and only if the track number is a single digit, add a leading zero. I'm gonna get some cringes from the audience here, but please be gentle; I'm still very much a beginner, this is how the code for that looks at the moment, and spoiler alert, though no surprise to some, it doesn't work:
[if..then block begins]
elif [[ $filename =~ ^[0-9]+\ .+\.mp3$ ]]; then
local track_number=$(echo "$filename" | cut -d' ' -f1)
local noscript=$(echo "$filename" | cut -d' ' -f2-)
# Add leading zero if track number is a single character
if [[ ${#track_number} -eq 1 ]]; then
track_number="0$track_number"
fi
local new_filename="${track_number} ${noscript}"
if [[ "$filename" != "$new_filename" ]]; then
mv "$mp3_file" "$sub_dir/$new_filename"
log "Renamed: $filename --> $new_filename"
((files_renamed++))
else
log "No changes needed for: $filename"
fi
[if..then block continues]
I have plenty of tracks that are named like "1 Kryptonite.mp3" which I want renamed to "01 Kryptonite.mp3". I have close to 10,000 tracks in my library, so I'm not doing this by hand for the first 9 tracks of 677 albums.
Where am I going wrong, style aside? (I'm literally just trying to get this to work before I make it pretty)
And yes, everything is correctly declared and everything; this is the only part of the noscript I'm having issue with; the rest of the renaming conditions work fine.
https://redd.it/13wviwz
@r_bash
Reddit
r/bash on Reddit: Is this regex correct for what I'm trying to do?
Posted by u/StrangeCrunchy1 - 1 vote and 4 comments
Is Recursion Stateful or Stateless?
Is recursion a stateful or stateless process?
On one hand, all previous iterations affect the next ones.
On the other hand, the function has no idea that it has ever been called before.
https://redd.it/13x4sbu
@r_bash
Is recursion a stateful or stateless process?
On one hand, all previous iterations affect the next ones.
On the other hand, the function has no idea that it has ever been called before.
https://redd.it/13x4sbu
@r_bash
Reddit
r/bash on Reddit: Is Recursion Stateful or Stateless?
Posted by u/Independent-Park9987 - No votes and no comments
How to use scale in bc (for division) within a bash noscript and set it as a variable?
Hi, I am modifying a brightness volume bash noscript for my i3wm setup. The original noscript requires xbacklight which doesnt work on my system, so I need to tweak it a little. Anyway, this part of the noscript:
function show_brightness_notif {
brightness=${get_brightness}
# some more codes below for dunstify
}
The idea is that I need to divide this brightness by the max brightness, so I can get a percentage. I found out that you can do float arithmetic using bc, so:
function show_brightness_notif {
brightness=${get_brightness}
bc -l <<< $brightness/$max_brightness
}
This `max_brightness` is set outside the function as global, it is a fixed number. If I do this, calling the function from the command line will indeed show the decimal result. Problem is: it has a lot of decimal places. A quick search shows that you can limit decimal place using scale: [https://askubuntu.com/questions/217570/bc-set-number-of-digits-after-decimal-point](https://askubuntu.com/questions/217570/bc-set-number-of-digits-after-decimal-point)
But where do I write this `scale` variable? More specifically, how do I put everything (the division using bc, the scale) into 1 variable? Ideally, I would use dunst to output this variable.
https://redd.it/13xip20
@r_bash
Hi, I am modifying a brightness volume bash noscript for my i3wm setup. The original noscript requires xbacklight which doesnt work on my system, so I need to tweak it a little. Anyway, this part of the noscript:
function show_brightness_notif {
brightness=${get_brightness}
# some more codes below for dunstify
}
The idea is that I need to divide this brightness by the max brightness, so I can get a percentage. I found out that you can do float arithmetic using bc, so:
function show_brightness_notif {
brightness=${get_brightness}
bc -l <<< $brightness/$max_brightness
}
This `max_brightness` is set outside the function as global, it is a fixed number. If I do this, calling the function from the command line will indeed show the decimal result. Problem is: it has a lot of decimal places. A quick search shows that you can limit decimal place using scale: [https://askubuntu.com/questions/217570/bc-set-number-of-digits-after-decimal-point](https://askubuntu.com/questions/217570/bc-set-number-of-digits-after-decimal-point)
But where do I write this `scale` variable? More specifically, how do I put everything (the division using bc, the scale) into 1 variable? Ideally, I would use dunst to output this variable.
https://redd.it/13xip20
@r_bash
Ask Ubuntu
bc: set number of digits after decimal point
bc handles numbers as integers:
# echo "100/3" | bc
33
bc -l handles numbers as floating point objects:
# echo "100/3" | bc -l
33.33333333333333333333
Is there a way to limit the number of digits...
# echo "100/3" | bc
33
bc -l handles numbers as floating point objects:
# echo "100/3" | bc -l
33.33333333333333333333
Is there a way to limit the number of digits...
Ksh etc. in 2023?
What do you guys think about still using a shell like the C shell or the Ksh shell? Are they still worth using?
https://redd.it/13xjds0
@r_bash
What do you guys think about still using a shell like the C shell or the Ksh shell? Are they still worth using?
https://redd.it/13xjds0
@r_bash
Reddit
r/bash on Reddit: Ksh etc. in 2023?
Posted by u/ignxcy - No votes and no comments
Adding the value of an array to a variable
Im just learning bash so this question mighta bit stupid. What im trying to do, is to add the values of every colum of an array, that contain 100 letters at maximum, to an Variable and give out the sum. For this i have created the following noscript:
When executing i get the following error message:
line 5: 0 + : syntax error: operand expected (error token is "+ ")
How can i fix this?
https://redd.it/13xry3j
@r_bash
Im just learning bash so this question mighta bit stupid. What im trying to do, is to add the values of every colum of an array, that contain 100 letters at maximum, to an Variable and give out the sum. For this i have created the following noscript:
#!/bin/bash read save[1000] i=0 sum=0 while (($i < 1000)); do sum=$(($sum + ${save[$i]})) ((i++)) done echo $sumWhen executing i get the following error message:
line 5: 0 + : syntax error: operand expected (error token is "+ ")
How can i fix this?
https://redd.it/13xry3j
@r_bash
Reddit
r/bash on Reddit: Adding the value of an array to a variable
Posted by u/cows_are_underrated_ - No votes and no comments