r_bash – Telegram
On my system, why do some commands print their output in a read-only mode that ends with (END), which I have to leave with a any-keypress? What is this and how can I turn this off once and for all?

noscript

https://redd.it/1k91qur
@r_bash
Efficiently delete a block of text containing a line matching regex pattern

File in the format:

General

StartWithLastProfile=1

Profile0
Name=default
IsRelative=1
Path=Profiles/default.cta

Profile1
Name=alicew
IsRelative=0
Path=D:\Mozilla\Firefox\Profiles\alicew
Default=1

Profile2
Name=sheldon
IsRelative=0
Path=D:\Mozilla\Firefox\Profiles\sheldon

How to delete entire block of text (delimited by an empty line) if line matches Name=alicew? It can be assumed there's only one unique match. So the file should be overwritten as:


General

StartWithLastProfile=1

Profile0
Name=default
IsRelative=1
Path=Profiles/default.cta

Profile2
Name=sheldon
IsRelative=0
Path=D:\Mozilla\Firefox\Profiles\sheldon

Preferably efficiently (i.e. requires only reading the file once) and in something relatively easy to understand and extend like awk or bash.

https://redd.it/1k9msmj
@r_bash
inotify use cases, generic app reloader responding to config changes

I'm looking for a way to automatically/efficiently do things when certain files change. For example, reload the status bar or notification application when their config changes. `inotify` seems appropriate for that, checking for changes as events instead of constantly polling with e.g. `sleep 1` in an indefinite loop (if the info you're looking to update changes rarely, the former would be much more efficient).

* Is the following suitable for a generic app reloader on config change and can it be improved? `app_reloader` is the most app-specific part of the implementation--some apps take a signal to reload the config without restarting the process, but the "generic" way would be to simply restart the process.

# This specific example is hardcoded for `waybar`, can/should it work for any
apps in general?

app_config="$HOME/.config/waybar" # App's dir to check for changes
app_cmd() { exec waybar & } # Command to start app

# Reload app. Usually means kill process and start new instance, but in this
example with waybar, signal can be sent to simply reload the config without
restarting the process
app_reload() {

killall -u "$USER" -SIGUSR2 waybar

# Wait until the processes have been shut down
# while pgrep -u "$UID" -x waybar > /dev/null; do sleep 1; done

}

while true; do
pgrep -u "$UID" -x waybar &>/dev/null || app_cmd

# Exclude hidden files sometimes created by text editors as part of
# periodic autosaves which could trigger an unintended reload
inotifywait -e create,modify -r "$app_config" --exclude "$app_config/\."

app_reload

done


* Is it a good idea to make heavy use of inotify throughout the filesystem? For example, checking `~/downloads` for when files complete their downloads (e.g if a `.part*`,`aria2`, etc. file no longer exists) and updating that count on the on the status bar (or similarly, do a `du -sh` only when a file is finished downloading, as opposed to status bars typically polling every 3-30 seconds).

* Also interested in any other ideas to take advantage of `inotify`--it seems heavily underutilized for some reason.

https://redd.it/1k9xhsf
@r_bash
comparing 2 sets of variables?

My code is unfortunately not working. It appears that it is only looking at the last 2 variables:

for reference a matches b and x matches y. I am attempting to compare the first 2 (I want a and b to match each other) and match the last 2 (I want x and y to match) if either set does not match, I want it to echo "no match".

if [[ "$a" == "$b" && "$x" == "$y" \]\];

then

echo "match"

else

echo "no match"

fi

https://redd.it/1kbkph0
@r_bash
Do you still write pure Bash, or do you mix in other tools?

At what point do you ditch Bash for Python, Go, or something else? Curious where others draw the line.

https://redd.it/1kbhtxo
@r_bash
orgsync - Keep your org files in sync across computers!

# orgsync

Hello everyone, after just 2 days of learning Bash I decided to get my hands dirty and start work on a project, one that would actually benefit me, and so I settled on orgsync. orgsync is a Bash CLI tool that makes it easier to keep my Emacs org files synced across devices, regardless of the OS type.

Initially, it started out from a need to facilitate syncing changes across my personal & work computers, both of which have different operating systems (MacOS & Windows), additionally, I recently had my work computer switched to a newer one, so I figured having an automated noscript that pulled my org repository and placed it in a previously defined location would be useful for future such cases.

Note that currently, on the Windows side of things, it only supports WSL, although msys support through Git Bash is something I want to implement.

The link to the repo is here: https://github.com/MiEdCaLe/orgsync

# Why?

Learning purposes, which is why it's in a very rough state and even though I realized halfway through that it might not be all that useful compared to just doing it manually, I learned a lot, and wanted to share it here!

https://redd.it/1kc0g8q
@r_bash
Sausage, a terminal word puzzle in Bash, inspired by Bookworm
https://redd.it/1kc5qsf
@r_bash
Mass renaming and moving of files according to file structure?

Hi,

I have a bunch of videos organised like this:

Videos
> Friends
> Season 1
> ep1.mp4
> ep2.mp4
> ep3.mp4
> Season 2
> ep1.mp4
> ep2.mp4
> ep3.mp4
> Season 3
> ep1.mp4
> ep2.mp4
> ep3.mp4

Now I want all files renamed according to file structure and moved to parent directory, like this:

Videos
> Friends_Season_1_ep1.mp4
Friends_Season_1_ep2.mp4
Friends_Season_1_ep3.mp4
Friends_Season_2_ep1.mp4
Friends_Season_2_ep2.mp4
Friends_Season_2_ep3.mp4
Friends_Season_3_ep1.mp4
Friends_Season_3_ep2.mp4
Friends_Season_3_ep3.mp4

How can I do that?

Thanks.

https://redd.it/1kc9g9p
@r_bash
How to make false && false fail in Bash Strict Mode?

How to make false && false fail in Bash Strict Mode?

Example:

#!/usr/bin/env bash
# Bash Strict Mode: https://github.com/guettli/bash-strict-mode
trap 'echo -e "\n🤷 🚨 🔥 Warning: A command has failed. Exiting the noscript. Line was ($0:$LINENO): $(sed -n "${LINENO}p" "$0" 2>/dev/null || true) 🔥 🚨 🤷 "; exit 3' ERR
set -Eeuo pipefail

false && false

echo foo



https://redd.it/1kcweni
@r_bash
text variable manipulation without external commands

I wish to do the following within bash, no external programs.

I have a shell variable which FYI contains a snooker frame score. It looks like the 20 samples below. Let's call the shell variable score. It's a scalar variable.

13-67(63) 7-68(68) 80-1 10-89(85) 0-73(73) 3-99(63) 97(52)-22 113(113)-24 59(59)-60(60) 0-67(57) 1-97(97) 120(52,56)-27 108(54)-0 130(129)-4 128(87)-0 44-71(70) 87(81)-44 72(72)-0 0-130(52,56) 90(66)-12

So we have the 2 players score separated by a "-". On each side of the - is possibly 1 or 2 numbers (separated by comma) in brackets "()". None of the numbers are more than 3 digits. (snooker fans will know anything over 147 would be unusual).

From that scalar score, I want six numbers, which are:


1: player1 score

2: player2 score

3: first number is brackets for p1

4: second number in brackets for p1

5: first number is brackets for p2

6: second number in brackets for p2

If the number does not exist, set it to -1.


So to pick some samples from above:

"13-67(63)" --> 13,67,-1,-1,63,-1

"120(52,56)-27" --> 120,27,52,56,-1,-1

"80-1" --> 80,1,-1,-1,-1,-1

"59(59)-60(60)" --> 59,60,59,-1,60,-1

...


I can do this with combination of echo, cut, grep -o "some-regexes", .. but as I need do it for 000s of values, thats too slow, would prefer just to do in bash if possible.

https://redd.it/1kcyrna
@r_bash
What to teach in awk under 4 hours for Undergraduate Computer Science students?
https://redd.it/1kd2nti
@r_bash
find; not specificName AND .png

i want a random file that is not currentPaper and is a png. thsi does not work: what wrong?
selectionPaper=$(find "$selectionPath" . \( ! -name "$currentPaper" -a -name *.png \) | shuf -n 1)



https://redd.it/1kd730b
@r_bash
A command in my noscript does not run.

#!/bin/bash

for i in "$@"; do
case $i in
-W | --Wallpaper )
WALLPAPER="$2"
Hyprland & # Start Hyprland.
sleep 30s && # A Time-Delay to let Hyprland initialize.
alacritty --hold -e set-wal -w "$WALLPAPER" -c -n # Set Sysytem Theme and Wallpaper (Using "swww img" and "wal -i").
shift # Past argument with no value.
;;
-wh | --wlan-home )
WNet-Config -wh # Connect to the network.
shift # Past argument with no value.
;;
-wm | --wireless-mobile )
WNet-Config -wm # Connect to mobile hot-spot.
shift # Past argument with no value.
;;
-* | --* )
echo "Unrecognized argument ( $i )."
exit 1
;;
*)
;;
esac
shift
done

# Why would the alacritty --hold -e <noscript123> not work?

# (I don't use a login manager so maybe it has something to do with the fact it does not find a graphical interface even after Hyprland has started, somebody help please).

#



#



#



#

https://redd.it/1kefknv
@r_bash
What's your favorite non-obvious Bash built-in or feature that more people don't use?

For me, it’s trap. I feel like most people ignore it. Curious what underrated gems others are using?

https://redd.it/1kf5exc
@r_bash
Scriptting exam.

Hi everyone,

Hey everyone, I have an exam coming mid June in OS. I'm pretty bad in Bash and I have the feeling I am going to fail that exam if I try to do it by myself.

You could argue with me to study, but I am a night student, so basically I go to Uni after work. I have a family and honestly sometimes 0 minutes to study. If I have the time, I rather study a subject with more credit points.

Regardless the teacher is super cool and basically allow us to go online for the exam. We have full access to Internet, to chat or to whatever it is. So I was wondering if you guys have an idea how I could pass this exam. I was thinking about GPT or something like that.

The exam will be centered around noscripting. The teacher also said to us in advance that GPT is OK no problem with that but if he sees two identical noscripts, he's going to fail the two student. Like I said he's super cool, so we have access to all the tools online and I was wondering guys if you have any advice.

https://redd.it/1kf6t89
@r_bash
Advice/ Brainstorm for my exam.

Hi everyone,

Hey everyone, I have an exam coming mid June in OS. I'm pretty bad in Bash and I have the feeling I am going to fail that exam if I try to do it by myself.

You could argue with me to study, but I am a night student, so basically I go to Uni after work. I have a family and honestly sometimes 0 minutes to study. If I have the time, I rather study a subject with more credit points.

Regardless the teacher is super cool and basically allow us to go online for the exam. We have full access to Internet, to chat or to whatever it is. So I was wondering if you guys have an idea how I could pass this exam. I was thinking about GPT or something like that.

The exam will be centered around noscripting. The teacher also said to us in advance that GPT is OK no problem with that but if he sees two identical noscripts, he's going to fail the two student. Like I said he's super cool, so we have access to all the tools online and I was wondering guys if you have any advice.

https://redd.it/1kf6n40
@r_bash
Converting Bat to Bash

I am wanting to convert a bat noscript into bash and I want to ensure this is right.

If someone could review the changes and let me know if this is proper that would be absolutely amazing.

Commenting Below the Original code then Converted code

----------------------------------------------

@echo off
noscript COS Regional Flasher
echo.**********************************************************************
echo.
echo. Oneplus 13 - COS Regional Flasher
echo. Originally two noscripts by FTH PHONE 1902 and Venkay
echo. modified by docnok63 and Jonas Salo
echo.
@echo off

cd %~dp0
set fastboot=Platform-Tools\fastboot.exe
if not exist "%fastboot%" echo "%fastboot%" not found. & pause & exit /B 1
set file=vendor_boot
echo.************************ START FLASH ************************
%fastboot% --set-active=a

:: Flash the fastboot images first
%fastboot% flash boot COS_FILES_HERE\boot.img
%fastboot% flash dtbo COS_FILES_HERE\dtbo.img
%fastboot% flash init_boot COS_FILES_HERE\init_boot.img
%fastboot% flash modem COS_FILES_HERE\modem.img
%fastboot% flash recovery COS_FILES_HERE\recovery.img
%fastboot% flash vbmeta COS_FILES_HERE\vbmeta.img
%fastboot% flash vbmeta_system COS_FILES_HERE\vbmeta_system.img
%fastboot% flash vbmeta_vendor COS_FILES_HERE\vbmeta_vendor.img
%fastboot% flash vendor_boot COS_FILES_HERE\vendor_boot.img

:: Check if super.img exists
if exist "super.img" (
%fastboot% flash super super.img
) else (
echo super.img not found. Skipping super.img...
)

:: Reboot to fastbootd
%fastboot% reboot fastboot
echo. ******************* REBOOTING TO FASTBOOTD *******************
ECHO #################################
ECHO # Hit English on Phone #
ECHO #################################
pause

:: Excluded files list (these should not be flashed again)
set excluded_images=boot.img dtbo.img init_boot.img modem.img recovery.img vbmeta.img vbmeta_system.img vbmeta_vendor.img vendor_boot.img my_bigball.img my_carrier.img my_company.img my_engineering.img my_heytap.img my_manifest.img my_preload.img my_product.img my_region.img my_stock.img odm.img product.img system.img system_dlkm.img system_ext.img vendor.img vendor_dlkm.img

:: Loop through all .img files in COS_FILES_HERE but skip excluded images
for %%G in (COS_FILES_HERE\*.img) do (
echo %excluded_images% | findstr /i /c:"%%~nxG" >nul
if errorlevel 1 (
echo Flashing %%~nG...
%fastboot% flash --slot=all "%%~nG" "%%G"
)
)

:: Define partitions list outside the IF block
set "partitions=my_bigball my_carrier my_engineering my_heytap my_manifest my_product my_region my_stock odm product system system_dlkm system_ext vendor vendor_dlkm my_company my_preload"

:: Check if super.img exists, if not, delete, create & flash logical partitions
if not exist "super.img" (
for %%P in (%partitions%) do (
%fastboot% delete-logical-partition %%P_a
%fastboot% delete-logical-partition %%P_b
%fastboot% delete-logical-partition %%P_a-cow
%fastboot% delete-logical-partition %%P_b-cow
%fastboot% create-logical-partition %%P_a 1
%fastboot% create-logical-partition %%P_b 1
%fastboot% flash %%P COS_FILES_HERE\%%P.img
)
) else (
echo super.img found. Logical partition flashes skipped...
)

echo.********************** CHECK ABOVE FOR ERRORS **************************
echo.************** IF ERRORS, DO NOT BOOT INTO SYSTEM **********************

:: Ask if user wants full Chinese bloat or not
choice /C YN /M "Do you want full chinese bloat?:"

if errorlevel 2 (
echo ****************** FLASHING OOS .305 my_preload ******************
%fastboot% delete-logical-partition my_preload_a
%fastboot% delete-logical-partition my_preload_b
%fastboot% delete-logical-partition my_preload_a-cow
%fastboot% delete-logical-partition my_preload_b-cow
%fastboot% create-logical-partition my_preload_a 1
%fastboot% create-logical-partition my_preload_b 1
%fastboot% flash my_preload
OOS_FILES_HERE\my_preload.img
echo ********* Debloat image flashed, Hit any key to continue *********
pause
) else (
echo ********************* CHINESE BLOAT ALREADY FLASHED **************************
echo ********* Keeping bloated my_preload, Hit any key to continue *********
pause
)

:: If super.img was not flashed, exit here but keep window open
if not exist "super.img" (
choice /C YN /M "Do you want to wipe data?:"

if errorlevel 2 (
echo *********************** NO NEED TO WIPE DATA ****************************
echo ***** Flashing complete. Hit any key to reboot the phone to Android *****
pause
%fastboot% reboot
exit /B 0
)

if errorlevel 1 (
echo ****************** FLASHING COMPLETE *****************
echo Wipe data by tapping Format Data on the screen, enter the code, and press format data.
echo Phone will automatically reboot into Android after wipe is done.
pause
exit /B 0
)
)

:: Ask if flashing from ColorOS (press Y for yes or N for no)
echo Are you flashing from ColorOS or Want to WIPE DATA?? (y/n)
choice /c YN /n > nul

:: Check if the user pressed 'y' or 'n'
if errorlevel 2 (
echo *********************** NO NEED TO WIPE DATA ****************************
echo ***** Flashing complete. Hit any key to reboot the phone to Android *****
pause
%fastboot% reboot
) else if errorlevel 1 (
echo ****************** FLASHING COMPLETE *****************
echo Wipe data by tapping Format Data on the screen, enter the code, and press format data.
echo Phone will automatically reboot into Android after wipe is done.
)

pause
----------------------------------------------

----------------------------------------------
Converted code
----------------------------------------------

----------------------------------------------
#!/bin/bash

# Set noscript (not directly equivalent in bash, but can be simulated)
echo "COS Regional Flasher"

echo "**********************************************************************"
echo ""
echo " Oneplus 13 - COS Regional Flasher "
echo " Originally two noscripts by FTH PHONE 1902 and Venkay"
echo " modified by docnok63 and Jonas Salo"
echo ""

# Get the directory of the noscript
SCRIPT_DIR=$(dirname "$0")

# Set fastboot path
FASTBOOT="$SCRIPT_DIR/Platform-Tools/fastboot"

# Check if fastboot exists
if [ ! -x "$FASTBOOT" ]; then
echo "Error: $FASTBOOT not found."
exit 1
fi

# Set file (not used in the original noscript, so keeping it as a variable)
FILE="vendor_boot"

echo "************************ START FLASH ************************"

# Flash the fastboot images first
$FASTBOOT --set-active=a

$FASTBOOT flash boot COS_FILES_HERE/boot.img
$FASTBOOT flash dtbo COS_FILES_HERE/dtbo.img
$FASTBOOT flash init_boot COS_FILES_HERE/init_boot.img
$FASTBOOT flash modem COS_FILES_HERE/modem.img
$FASTBOOT flash recovery COS_FILES_HERE/recovery.img
$FASTBOOT flash vbmeta COS_FILES_HERE/vbmeta.img
$FASTBOOT flash vbmeta_system COS_FILES_HERE/vbmeta_system.img
$FASTBOOT flash vbmeta_vendor COS_FILES_HERE/vbmeta_vendor.img
$FASTBOOT flash vendor_boot COS_FILES_HERE/vendor_boot.img

# Check if super.img exists
if [ -f "super.img" ]; then
$FASTBOOT flash super super.img
else
echo "super.img not found. Skipping super.img..."
fi

# Reboot to fastbootd
$FASTBOOT reboot fastboot
echo " ******************* REBOOTING TO FASTBOOTD *******************"
echo "#################################"
echo "# Hit English on Phone #"
echo "#################################"

read -p "Press Enter to continue..."

# Excluded files list
EXCLUDED_IMAGES="boot.img dtbo.img init_boot.img modem.img recovery.img vbmeta.img vbmeta_system.img vbmeta_vendor.img vendor_boot.img my_bigball.img my_carrier.img my_company.img my_engineering.img my_heytap.img my_manifest.img my_preload.img my_product.img my_region.img my_stock.img odm.img product.img system.img system_dlkm.img system_ext.img vendor.img