r_bash – Telegram
An optimized way of checking / adding text to a file

I have a batch noscript. When I run that batch noscript, I want it to open a config file and check for two specific lines that are usually next to each other

myconf.cnf

phpengine
# For debugging only #
show-all-errors

What I'm trying to do is check if [phpengine] and #show-all-errors exist. (the show-all-errors has to be below [phpengine]

And when matching those two lines, a way to toggle show-all-errors to be either commented or uncommented.

So I pretty much need the bash noscript to just check the file and see if those two lines exist, and then have control over adding a # in front of #show-all-errors or removing the comment character.

I started trying to write code to add / remove the comment based on a matching line, but then I realized all the extra conditions

How to do it when there's a commented line in-between separating the two lines.
Adding the text to the file if the two lines don't exist.

So I'm not sure where the heck to go with this. And I could remove the commented line "#For debugging only #", but the noscript will auto-readd it on update, and I wanted a solution to just have it run and take care of it for me without manual edits to remove the line.

And the code I've got now really isn't crap, because all it does is detect if it's commented or uncommented

    if sudo grep -Eq '^ *show-all-errors' "/etc/php-engine/myconf.cnf"; then
# found match
fi


And I found some helpers for toggling comments on / off but this seems over-kill, and I doubt I'm even going in the right direction

    comment()
{
local regex="${1:?}"
local file="${2:?}"
local comment_mark="${3:-#}"
sed -ri "s:^([ ]*)($regex):\\1$comment_mark\\2:" "$file"
}

uncomment()
{
local regex="${1:?}"
local file="${2:?}"
local comment_mark="${3:-#}"
sed -ri "s:^([ ]*)[$comment_mark]+[ ]?([ ]*$regex):\\1\\2:" "$file"
}


https://redd.it/179pzc6
@r_bash
A student in need for help

I'm trying to create a little program as an exercise that let the user input as many characters as possible in x seconds to calculate and output charactes per minutes.

I'm having a little problem that chatgpt can't solve. The timer in backgroung won't stop whaterver i try.

Thanks in advance

#!/bin/bash

clear

t=1
countdown() {
local seconds=$1
while (( $seconds >= 0 ))
do
sleep 1
((seconds--))
done
t=0
}

duration=5
countdown $duration &

echo "Enter characters within $duration seconds"

count=0
while (( t != 0 ))
do

read -sn 1 input
clear
((conta++))
echo $count

done

echo "Time expired"
sleep 1
clear

kill $1

echo "You entered: $count characters in $duration seconds"


​

https://redd.it/179v1rv
@r_bash
GitComic (Git Commit Icons)

I've just created a noscript that allows you to insert an icon 🙂 in your commits based on the type of modifications you've made in your repository.

If you want to start organizing your commits in a more professional 🧠 yet fun 🤪 way, head over here and take a look at the README. There, you'll find all the ℹ️ information you need. I'm sure you'll love it.

Example video 🎥 in the repo.

https://redd.it/17ad1hr
@r_bash
A student in need for help

#!/bin/bash

clear

t=$(date +%s)
duration=5
t=$(($t + $duration))

echo "Enter characters within $duration seconds"

count=0
while [ $(date +%s) -le $t ]
do
read -sn 1 input
clear
((count++))
echo $count
done

echo "Time expired"
sleep 2
clear


echo -e "You entered: $count characters in $duration seconds\n"

speed=$(($count * (60 / $duration)))

echo -e "Your TAPPING speed is $speed clicks per minute\n"

The program worked just fine until i tried not giving it any input, i noticed it stays in the while loop at the read forever and never gets to check again if the time passed.

What are your ideas, how could i write it so it constantly checks the passage of time?

https://redd.it/17amor3
@r_bash
Saving history, redux

I had posted several months ago about a problem I was seeing in git-bash on Windows. It involved saving history on exit.

The method I had been using was to have a noscript file that created a randomized file name based on date and save the history to that file. In my .bashrc, I had

trap ./bin/savehistory.sh EXIT,

and that noscript

$ cat bin/save\
history.sh

#!/bin/bash
HISTFILE="/c/Users/ME/.hist/$(date +%d%b%y%H%M)$RANDOM"
history >& $HISTFILE

If I ran that noscript from the bash prompt, it would create an empty file with the correct name in the .hist directory.

I tried a lot of things to make this work, to no avail. Note that this still works on Linux.

Yesterday, I tried something different.

From my .bashrc

alias svh='history > "/c/Users/ME/.hist/$(date +%d%b%y%H%M)$RANDOM"
trap svh EXIT

This works on Windows git-bash.

https://redd.it/17auhmn
@r_bash
How do I create a short-cut for a command in the shell config file?

From this:

We can create even a short‐cut for this command in your shell configuration file, which is either \~/.bashrc or \~/.profile:

># inspect the first and last 2 lines of a file
>
>i() { (head -n 2; tail -n 2) < "$1" | column -t}

I tried placing the above line of code in the bashrc and source it but it throws a syntax error:

syntax error: unexpected end of file

What exactly does this mean and how do I go about solving this? Where does the input go in this case?

&#x200B;

Thanks!

https://redd.it/17b0aiy
@r_bash
Fuzzy find Gitmojis with fzf

I wrote another emoji noscript 🎉 This time for printing gitmojis in the terminal or vim.

&#x200B;

* jq to filter gitmoji API

* fzf to fuzzy find

* curl to fetch Data

* vimrc func to paste within vim

* works with or without tmux session (fzf, fzf-tmux)

&#x200B;

Gitmoji git commit convention: https://gitmoji.dev/

Script: https://github.com/SimonWoodtli/dotfiles/blob/main/noscripts/gitmoji

Demo: https://gifyu.com/image/S6r4A

https://redd.it/17b8qxm
@r_bash
Bash tutorial hell.

https://unix.stackexchange.com/questions/759147/automate-traversing-a-directory-in-linux
I ask questions like these online. Problem being I don't yet understand the concepts and commands that they are using there.
So, I thought a good idea will be to keep continuing my bash education from udemy. I am continuing my course by Ralph Jensen and already through 80% of the course.

Few commands remaining are test commands, conditional constructs and symbolic links. But I feel this course hasn't done a good job preparing me for bash noscripting.

I've documented everything that I've learnt some place.

Any tips? The general advice seems to be to just automate stuffs you need irl. But not clear instruction and helpful.

https://redd.it/17bcxyb
@r_bash
/usr/bin/ffmpeg: cannot execute binary file

i have this noscript(lel.sh) to take an input file and map its video streams(only actual video, not attached pictures) and map audio if any, remove subnoscripts so i can map my own subs and lastly a thumbnail file to an output file:

#!/bin/bash -x

. ffmpeg -i "$1" -i "$2" -i "$3" -map 0:V -map 0:a? -map -0:s? -map 1 -map 2 -c copy -c:s mov_text -disposition:v:1 attached_pic -metadata noscript=\""$4"\" "$4.mp4"

# test

$ lel.sh Atonement\ \(2007\).mp4 subs.srt thumb.jpg "Atonement"

# output

+ . ffmpeg -i 'Atonement (2007).mp4' -i subs.srt -i thumb.jpg -map 0:V -map '0:a?' -map '-0:s?' -map 1 -map 2 -c copy -c:s mov_text -disposition:v:1 attached_pic -metadata 'noscript="Atonement"' Atonement.mp4

/home/anon/.local/bin/lel.sh: line 2: .: /usr/bin/ffmpeg: cannot execute binary file

also, for some reason if i run sh ~/.local/bin/lel.sh Atonement\ \(2007\).mp4 subs.srt thumb.jpg "Atonement" i get:

/home/anon/.local/bin/lel.sh: 1: /usr/bin/ffmpeg: Syntax error: "(" unexpected

https://redd.it/17bf6db
@r_bash
How to combine dowloading files and compressing them simultaneously?

The software fasterq-dump doesn't offer zipping fastq/sra files. How can I add bgzip/gzip for a long list of paired end sequencing files (the files are split into something like SRR*****_1.fastq and SRR*****_2.fastq) without having to compress them after the download is done?

I wrote a line of code to download files using parallel as well as a for loop but not sure how the gzip/bgzip would work and where to use it for file compression:

cat $SRR_LIST | parallel --verbose fasterq-dump -p -v --split-files --qual-defline '+' {} --outdir "$dir/ChIP-Seq/"

or

for SRR in ${SRR_LIST}

do

echo Downloading ${SRR}...

#time fasterq-dump -p -v --split-files --qual-defline '+' ${SRR} --outdir "$dir/ChIP-Seq/"

echo Done ${SRR}.

done

https://redd.it/17bjobh
@r_bash
Enjoy with Best Service IPTV Provider in The world 2023

**http://etsy-tv.store**

"Say goodbye to expensive cable bills and hello to unlimited entertainment with our TV service. Get access to over +19.000 channels from around the world at a fraction of the cost."

"Cut the cord and join the TV revolution. Our service offers the best in live TV streaming, with crystal-clear picture quality and reliable service. Sign up now to start enjoying your favorite shows, sports, and movies."

"Why settle for limited cable options when you can have it all with TV? Our service delivers premium channels from all over the world, including sports, movies, and TV shows. Plus, with our easy-to-use interface, you'll never miss a beat."

"Looking for a better way to watch TV? Look no further than our TV service. With our extensive channel lineup, including international options, you'll have access to all your favorites. Say goodbye to cable and hello to the future of television."

I know that this group is not dedicated to T.V services, but I just want to present my service to those who are interested. Due to that, I have received negative karma on my account.

https://redd.it/17bkhhc
@r_bash
Router noscript?

I own an asus gtax11000 pro router and it's possible to tell it to reboot at night. However this isn't always a good thing if i am downloading or busy transferring files across the network at the time.

Is it possible to write a noscript and execute it on the router that would detect if there was any wan and/or lan activity in the past 10 minutes and if not then send a command to restart the router?

https://redd.it/17cc28f
@r_bash
Saving variables to file

Ok so say I did this

Echo “this variable will be saved”

Read variable

How do I save that variable to a text file,what would be the syntax for it? Thanks

https://redd.it/17cbsbp
@r_bash
Backend Question Is dup2 how a UNIX shell, like Bash, does things like cat /dev/fd/5 5<<< foo?

So I wanna know how a shell like Bash does this:

cat /dev/fd/5 5<<< "foo"


All I can guess is, it first opens file denoscriptor 3, just by calling open(2), then duplicates it to fd 5 using dup2(2) as in: dup2(fd, 5). Is that how it does this?

Thanks for your help.
Edit: I did strace, there was not a SINGLE 5 in there!

https://redd.it/17ckjh6
@r_bash
listing files in correct numeric order?

I have a directory that has files named like this:

S01E001.mp4
S01E002.mp4
S01E003.mp4
S01E004.mp4
S01E005.mp4
S01E006.mp4
S01E007.mp4
S01E008.mp4
S01E009.mp4
S01E010.mp4
S01E011.mp4
S01E012.mp4
S01E013.mp4

However, ls gives this output:

S01E010.mp4
S01E011.mp4
S01E001.mp4
S01E002.mp4
S01E003.mp4
S01E004.mp4
S01E005.mp4
S01E006.mp4
S01E007.mp4
S01E008.mp4
S01E009.mp4
S01E012.mp4
S01E013.mp4

Why does this occur?

https://redd.it/17cnflj
@r_bash
How to define parameters in a function in BASH?

Hello, I'm trying to write a Knight's Tour resolutor noscript in BASH but I need to apply recursion and don't know how without function parameters. For example, I want to use two functions, one that resolves the algorithm (knight_tour)) and a "main". I want to use recursion inside the knight_tour() function but I don't know how without parameters. I'm very new to BASH so I know it's atrocious. Thank you even for just reading!


for i in {1..8}

do

prossimo_x=$((x+cavallo_movimento_x[i]));

prossimo_y=$((y+cavallo_movimento_y[i]));

scacchiera[prossimo_x, prossimo_y]=num_mossa;

&#x200B;

knight_tour $prossimo_x $prossimo_y $((num_mossa+1)) $scacchiera $cavallo_movimento_x $cavallo_movimento_y

&#x200B;

https://redd.it/17cpidl
@r_bash
Function Arguments/Recursion

Hi, I'm pretty new in bash. I have a question, can you implement recursion in a function? For example:
You have a function fun1(). Can you "manually add" arguments or you have to do some work-arounds? Can you do something like calling fun1(int a, int b, int c) inside fun1? I'm trying to implement Knight's Tour in Bash but it's so damn hard in Bash (school stuff, I can't use C) Thank you!

https://redd.it/17cq92s
@r_bash
Yay! Got my Certificate of absurdity.
https://redd.it/17cr5wn
@r_bash
make: Error 1

I am trying to make the build folder with command "make" but keep getting Error 1:

make: \
** [Makefile:54: build/server_multi.o\] Error 1

&#x200B;

Here is a glimpse of my Makefile:

&#x200B;

TARGETS = server_lim

LIBS = timelib

LDFLAGS = -lm -lpthread

BUILDDIR = build

BUILD_TARGETS = $(addprefix $(BUILDDIR)/,$(TARGETS))

OBJS = $(addprefix $(BUILDDIR)/,$(addsuffix .o,$(TARGETS) $(LIBS)))

LIBOBJS = $(addprefix $(BUILDDIR)/,$(addsuffix .o,$(LIBS)))

&#x200B;

all: $(BUILD_TARGETS)

&#x200B;

$(BUILD_TARGETS): $(BUILDDIR) $(OBJS)

gcc -o $@ $@.o $(LIBOBJS) $(LDFLAGS) -W -Wall

&#x200B;

$(BUILDDIR):

mkdir $(BUILDDIR)

&#x200B;

$(BUILDDIR)/%.o: %.c

gcc -o $@ -c $< -W -Wall

&#x200B;

clean:

rm *\~ -rf $(BUILDDIR)



How can I fix this error?

https://redd.it/17cvfju
@r_bash