's/\[/\\\[/g' -e 's/\]/\\\]/g' -e 's/ /\\\ /g' -e 's/\./\\\./g' -e 's/{/\\\{/g' -e 's/}/\\\}/g' -e 's/\!/\\\!/g' -e 's/\&/\\\&/g' `
#Escape out all special characters so it works in sed
for i in *
do
FNAME=`echo "$i" | sed s/"$SPATT"//`
if [ "$i" != "$FNAME" ]
then
mv "$i" "$FNAME"
fi
done
echo "done"
fi
#------------------Trim directory names after year
if echo "$OPT" | grep -q 't'
then
echo -n "Trimming directory names after noscript and year... "
for h in *
do
if [ -d "$h" ]
then
FNAME=`echo "$h" | sed 's/\[\ www\.Torrenting\.com\ \]\ \-\ //' | sed 's/1080//' | sed 's/1400//'`
EARLY="$FNAME"
FNAME=`echo "$FNAME" | sed 's/\(^.*([0-9]\{4\})\).*$/\1/'` #this won't do anything unless the year is in parentheses
if [ "$FNAME" = "$EARLY" ] #testing whether parentheses-dependent sed command did anything
then
FNAME=`echo "$FNAME" | sed 's/\(^.*[0-9]\{4\}\).*$/\1/'` #if not, trim after last digit in year
FNAME=`echo "$FNAME" | sed 's/\([0-9]\{4\}\)/(\1)/'` #and then add parentheses around year
mv "$h" "$FNAME" #and rename
else
mv "$h" "$FNAME" #if the parentheses-dependent sed worked, just rename it
fi
fi
done
rename 's/\[\(/\(/' *
rename 's/\(\(/\(/' *
echo "done"
fi
#------------------Match file names to parent directory names
if echo "$OPT" | grep -q 'm'
then
echo -n "Matching filenames to parent directory names and deleting junk files... "
for h in *
do
if [ -d "$h" ]
then
rename 's/ /_/g' "$h"#replace spaces in directory names
fi#with underscores so mv doesn't choke
done
for i in *
do
if [ -d "$i" ]
then
cd "$i"
for j in *
do
#replace spaces with underscores in all filenames in each subdirectory
rename 's/ /_/g' *
done
cd ..
fi
done
for k in *
do
if [ -d "$k" ]
then
cd "$k"#go into each directory
find ./ -regex ".*[sS]ample.*" -delete#take out the trash
NEWN="$k"#NEWN="directory name"
for m in *
do
EXTE=`echo $m | sed 's/^.*\(....$\)/\1/'`#read file extension into EXTE
if [ "$EXTE" = ".mp4" -o "$EXTE" = ".m4v" -o "$EXTE" = ".mkv" -o "$EXTE" = ".avi" ]
then
mv -n $m "./$NEWN$EXTE"
elif [ "$EXTE" = ".srt" ]
then
#check to see if .srt file is actually real
FISI=`du "$m" | sed 's/\([0-9]*\)\t.*/\1/'`
#is it real subnoscripts or just a few words based on file size?
if [ "$FISI" -gt 10 ]
then
mv -n $m "./$NEWN.eng$EXTE"#if it's legit, rename it
else
#if it's not, delete it
rm $m
fi
elif [ "$EXTE" = ".sub" -o "$EXTE" = ".idx" ]
then
mv -n $m "./$NEWN.eng$EXTE"
elif [ "$EXTE" = ".nfo" -o "$EXTE" = ".NFO" -o "$EXTE" = ".sfv" -o "$EXTE" = ".exe" -o "$EXTE" = ".txt" -o "$EXTE" = ".jpg" -o "$EXTE" = ".JPG" -o "$EXTE" = ".png" -o "$EXTE" = "part" ]
then
rm $m#delete all extra junk files
fi
done
cd ..
fi
done
#turn all the underscores back into spaces
#in directory names first...
rename 's/_/ /g' *
for n in *
do
if [ -d "$n" ]
then
cd "$n"
for p in *
do
rename 's/_/ /g' *#...and files within directories
done
cd ..
fi
done
fi
#---------------------List directories and files
echo
#Escape out all special characters so it works in sed
for i in *
do
FNAME=`echo "$i" | sed s/"$SPATT"//`
if [ "$i" != "$FNAME" ]
then
mv "$i" "$FNAME"
fi
done
echo "done"
fi
#------------------Trim directory names after year
if echo "$OPT" | grep -q 't'
then
echo -n "Trimming directory names after noscript and year... "
for h in *
do
if [ -d "$h" ]
then
FNAME=`echo "$h" | sed 's/\[\ www\.Torrenting\.com\ \]\ \-\ //' | sed 's/1080//' | sed 's/1400//'`
EARLY="$FNAME"
FNAME=`echo "$FNAME" | sed 's/\(^.*([0-9]\{4\})\).*$/\1/'` #this won't do anything unless the year is in parentheses
if [ "$FNAME" = "$EARLY" ] #testing whether parentheses-dependent sed command did anything
then
FNAME=`echo "$FNAME" | sed 's/\(^.*[0-9]\{4\}\).*$/\1/'` #if not, trim after last digit in year
FNAME=`echo "$FNAME" | sed 's/\([0-9]\{4\}\)/(\1)/'` #and then add parentheses around year
mv "$h" "$FNAME" #and rename
else
mv "$h" "$FNAME" #if the parentheses-dependent sed worked, just rename it
fi
fi
done
rename 's/\[\(/\(/' *
rename 's/\(\(/\(/' *
echo "done"
fi
#------------------Match file names to parent directory names
if echo "$OPT" | grep -q 'm'
then
echo -n "Matching filenames to parent directory names and deleting junk files... "
for h in *
do
if [ -d "$h" ]
then
rename 's/ /_/g' "$h"#replace spaces in directory names
fi#with underscores so mv doesn't choke
done
for i in *
do
if [ -d "$i" ]
then
cd "$i"
for j in *
do
#replace spaces with underscores in all filenames in each subdirectory
rename 's/ /_/g' *
done
cd ..
fi
done
for k in *
do
if [ -d "$k" ]
then
cd "$k"#go into each directory
find ./ -regex ".*[sS]ample.*" -delete#take out the trash
NEWN="$k"#NEWN="directory name"
for m in *
do
EXTE=`echo $m | sed 's/^.*\(....$\)/\1/'`#read file extension into EXTE
if [ "$EXTE" = ".mp4" -o "$EXTE" = ".m4v" -o "$EXTE" = ".mkv" -o "$EXTE" = ".avi" ]
then
mv -n $m "./$NEWN$EXTE"
elif [ "$EXTE" = ".srt" ]
then
#check to see if .srt file is actually real
FISI=`du "$m" | sed 's/\([0-9]*\)\t.*/\1/'`
#is it real subnoscripts or just a few words based on file size?
if [ "$FISI" -gt 10 ]
then
mv -n $m "./$NEWN.eng$EXTE"#if it's legit, rename it
else
#if it's not, delete it
rm $m
fi
elif [ "$EXTE" = ".sub" -o "$EXTE" = ".idx" ]
then
mv -n $m "./$NEWN.eng$EXTE"
elif [ "$EXTE" = ".nfo" -o "$EXTE" = ".NFO" -o "$EXTE" = ".sfv" -o "$EXTE" = ".exe" -o "$EXTE" = ".txt" -o "$EXTE" = ".jpg" -o "$EXTE" = ".JPG" -o "$EXTE" = ".png" -o "$EXTE" = "part" ]
then
rm $m#delete all extra junk files
fi
done
cd ..
fi
done
#turn all the underscores back into spaces
#in directory names first...
rename 's/_/ /g' *
for n in *
do
if [ -d "$n" ]
then
cd "$n"
for p in *
do
rename 's/_/ /g' *#...and files within directories
done
cd ..
fi
done
fi
#---------------------List directories and files
echo
"done"
echo
for i in *
do
if [ -f "$i" ]
then
echo -e "\033[34m$i\033[0m"
elif [ -d "$i" ]
then
echo -e "\033[32;4m$i\033[0m"
cd "$i"
for j in *
do
if [ -f "$j" ]
then
echo -e "\t\033[34m$j\033[0m"
elif [ -d "$j" ]
then
echo -e "\t\033[32;4m$j\033[0m"
fi
done
echo
cd ..
fi
done
echo
https://redd.it/1hz5ec8
@r_bash
echo
for i in *
do
if [ -f "$i" ]
then
echo -e "\033[34m$i\033[0m"
elif [ -d "$i" ]
then
echo -e "\033[32;4m$i\033[0m"
cd "$i"
for j in *
do
if [ -f "$j" ]
then
echo -e "\t\033[34m$j\033[0m"
elif [ -d "$j" ]
then
echo -e "\t\033[32;4m$j\033[0m"
fi
done
echo
cd ..
fi
done
echo
https://redd.it/1hz5ec8
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
I would really appreciate checking my first bash project which i spent some time creating it after learning bash.
https://github.com/irfanbroo/Air_QualityWeatherAPI_bash
https://redd.it/1hyzdpi
@r_bash
https://github.com/irfanbroo/Air_QualityWeatherAPI_bash
https://redd.it/1hyzdpi
@r_bash
GitHub
GitHub - irfanbroo/Air_QualityWeatherAPI_bash: This is a Bash noscript that fetches and displays weather and air quality reports…
This is a Bash noscript that fetches and displays weather and air quality reports for any city. It uses data from wttr.in and OpenWeatherMap, providing details like temperature, weather conditions, ...
Bad Matrix / Share your Bash sh-nanigans
Why? Because, when life in meatspace gets a bit too much, it's important for the sh-oul, to hor-sh around for sh-ts and giggles.
Throwback: a really bad "Matrix Rain" animation, in a very large Bash function, made with tput and urandom and faux-Kanji that may end up spelling swear words (but I don't want to know!) https://www.evalapply.org/posts/bad-matrix/
sh-ow yours!
https://redd.it/1hzryva
@r_bash
Why? Because, when life in meatspace gets a bit too much, it's important for the sh-oul, to hor-sh around for sh-ts and giggles.
Throwback: a really bad "Matrix Rain" animation, in a very large Bash function, made with tput and urandom and faux-Kanji that may end up spelling swear words (but I don't want to know!) https://www.evalapply.org/posts/bad-matrix/
sh-ow yours!
https://redd.it/1hzryva
@r_bash
www.evalapply.org
Bad Matrix
A while ago, someone in the Recurse Center nerdiverse decided we needed a "Bad Print". They made one. Things escalated. Bad Matrix happened.
I created "Command Runner", a library that helps you setting up a simple CI for your projects.
Hey guys,
that's my first post on reddit and this subreddit in particular, so I hope I get the format right ;)
I wanted to create a simple CI library for my repositories to run reoccurring commands repeatedly and have a nice report after execution. I came up with "Command Runner".
https://github.com/antonrotar/command\_runner
It provides a simple API and some settings to adjust execution and logging. It's basically a thin wrapper around commands and integrates nicely with larger scope tool setups like Github Actions.
Have a look! :)
https://redd.it/1i0b2st
@r_bash
Hey guys,
that's my first post on reddit and this subreddit in particular, so I hope I get the format right ;)
I wanted to create a simple CI library for my repositories to run reoccurring commands repeatedly and have a nice report after execution. I came up with "Command Runner".
https://github.com/antonrotar/command\_runner
It provides a simple API and some settings to adjust execution and logging. It's basically a thin wrapper around commands and integrates nicely with larger scope tool setups like Github Actions.
Have a look! :)
https://redd.it/1i0b2st
@r_bash
GitHub
GitHub - antonrotar/command_runner: A small library to setup a simple CI or installation noscript.
A small library to setup a simple CI or installation noscript. - antonrotar/command_runner
Need Help in Improving my noscript
So , I have a small project where i want to install a few things on my laptop , so i created a noscript to help me out , as a generic noscript.
But the thing is there are still a few thing i could need help with . please share your view and if possible please share it as a PR if you can . will help a lot
the Link to the repo: https://github.com/aniketrath/noscripts
https://redd.it/1i0mtci
@r_bash
So , I have a small project where i want to install a few things on my laptop , so i created a noscript to help me out , as a generic noscript.
But the thing is there are still a few thing i could need help with . please share your view and if possible please share it as a PR if you can . will help a lot
the Link to the repo: https://github.com/aniketrath/noscripts
https://redd.it/1i0mtci
@r_bash
GitHub
GitHub - aniketrath/noscripts: Scripts for easier Life
Scripts for easier Life. Contribute to aniketrath/noscripts development by creating an account on GitHub.
Trying to create install noscript for a rails app, struggling with if statements and multi line comments
I am trying to create an installation noscript to normalize development environments for a rails application.
I am struggling with this command:
certbot certonly \
--dns-cloudflare \
--dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini \
--dns-cloudflare-propagation-seconds 60 \
-d example.com
I do not understand how to use multiline comments with `\` inside the if statement below. I am properly doing something stupid wrong, but I can't figure it out.
if [ -e ~/.secrets/certbot/cloudflare.ini ]; then
echo -e "A Cloudflare token is already configured to be used by Certbot with DNS verification using Cloudflare. \nWe will try to request a certificate using following FQDN:"
echo $hostname
read -n 1 -s -r -p "Press any key to continue."
echo "We are now creating sample certificates using Let's Encrypt."
sudo certbot certonly \ --dns-cloudflare \ --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini \ --dns-cloudflare-propagation-seconds 60 \ -d $hostname
echo "The certificate has been created."
else
echo -e "Cloudflare is not yet configured to be used for Certbot, \nPlease enter your API token to configure following FQDN:"
echo $hostname
read cloudflaretoken
echo "We are now creating your file with the API token, you will find it in the following file: ~/.secrets/certbot/cloudflare.ini."
mkdir -p ~/.secrets/certbot/
touch ~/.secrets/certbot/cloudflaretest.ini
bash -c 'echo -e "# Cloudflare API token used by Certbot\ndns_cloudflare_api_token = $cloudflaretoken" > ~/.secrets/certbot/test.ini'
fi
https://redd.it/1i1fgsz
@r_bash
I am trying to create an installation noscript to normalize development environments for a rails application.
I am struggling with this command:
certbot certonly \
--dns-cloudflare \
--dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini \
--dns-cloudflare-propagation-seconds 60 \
-d example.com
I do not understand how to use multiline comments with `\` inside the if statement below. I am properly doing something stupid wrong, but I can't figure it out.
if [ -e ~/.secrets/certbot/cloudflare.ini ]; then
echo -e "A Cloudflare token is already configured to be used by Certbot with DNS verification using Cloudflare. \nWe will try to request a certificate using following FQDN:"
echo $hostname
read -n 1 -s -r -p "Press any key to continue."
echo "We are now creating sample certificates using Let's Encrypt."
sudo certbot certonly \ --dns-cloudflare \ --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini \ --dns-cloudflare-propagation-seconds 60 \ -d $hostname
echo "The certificate has been created."
else
echo -e "Cloudflare is not yet configured to be used for Certbot, \nPlease enter your API token to configure following FQDN:"
echo $hostname
read cloudflaretoken
echo "We are now creating your file with the API token, you will find it in the following file: ~/.secrets/certbot/cloudflare.ini."
mkdir -p ~/.secrets/certbot/
touch ~/.secrets/certbot/cloudflaretest.ini
bash -c 'echo -e "# Cloudflare API token used by Certbot\ndns_cloudflare_api_token = $cloudflaretoken" > ~/.secrets/certbot/test.ini'
fi
https://redd.it/1i1fgsz
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Help writing function/pipeline
Hi I'm relatevely new to bash and I use it mainly to process small data files. I've been using these commands to extract and reorder data from .cvs files, I've tried to write a single pipeline with the commands but so far I've been unable to properly add the sed command into the pipeline, everything works fine until the sed command needs to be used but if separate the pipeline before each sed everything works fine. So any help to integrate everything into a single pipeline or even to create a function would be great. Thank you in advance.
https://redd.it/1i0km45
@r_bash
Hi I'm relatevely new to bash and I use it mainly to process small data files. I've been using these commands to extract and reorder data from .cvs files, I've tried to write a single pipeline with the commands but so far I've been unable to properly add the sed command into the pipeline, everything works fine until the sed command needs to be used but if separate the pipeline before each sed everything works fine. So any help to integrate everything into a single pipeline or even to create a function would be great. Thank you in advance.
awk -F "\"*,\"*" '{print $2}' File1.csv| tail -n +2| paste -sd" " > File2.txtsed -i 's/ 0 /\n/g' File2.txtsed -i 's/ /\t/g' File2.txthttps://redd.it/1i0km45
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Change colour of double tab suggestions
I have been playing around with customising my bash prompt, just for fun, and it got me wondering if there's a way to alter the colour of the suggestions that appear when pressing double tab. Usually it will display all your options for filling in either the next file/directory, or your options for commands, on a separate line but in the same colour as the rest of the text. can I make it be a different colour to the rest?
https://redd.it/1i1o6rv
@r_bash
I have been playing around with customising my bash prompt, just for fun, and it got me wondering if there's a way to alter the colour of the suggestions that appear when pressing double tab. Usually it will display all your options for filling in either the next file/directory, or your options for commands, on a separate line but in the same colour as the rest of the text. can I make it be a different colour to the rest?
https://redd.it/1i1o6rv
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
My noscript uses more CPU than I think it should
I created the below noscript to turn off the keyboard light on my Lenovo Thinkpad P1 when I'm not typing.
https://gist.github.com/tonsV2/cc97bb6dd3fdd82e2e2961d417803eaa
However I see it at the top of my process list using close to 100% of CPU for a lot longer than I'd expect. Can anyone here tell me how to improve it?
https://redd.it/1i1q6u3
@r_bash
I created the below noscript to turn off the keyboard light on my Lenovo Thinkpad P1 when I'm not typing.
https://gist.github.com/tonsV2/cc97bb6dd3fdd82e2e2961d417803eaa
However I see it at the top of my process list using close to 100% of CPU for a lot longer than I'd expect. Can anyone here tell me how to improve it?
https://redd.it/1i1q6u3
@r_bash
Gist
Turn off keyboard lights when not typing
Turn off keyboard lights when not typing. GitHub Gist: instantly share code, notes, and snippets.
Bunster: compile bash noscripts to self contained executables
https://github.com/yassinebenaid/bunster
https://redd.it/1i26mav
@r_bash
https://github.com/yassinebenaid/bunster
https://redd.it/1i26mav
@r_bash
GitHub
GitHub - yassinebenaid/bunster: Compile shell noscripts to static binaries.
Compile shell noscripts to static binaries. Contribute to yassinebenaid/bunster development by creating an account on GitHub.
Filtering output while outputting it.
So the concept is simple. I have a complex command that generates output to the screen. Within that output is a single piece of data that I want to capture and use later, but not in such a way that it disrupts the flow of output to the screen. If the complex command's not interactive and relatively short, I've found I can do this:
declare OUTPUT=$(complex_command)
declare -i data_captured=$(sed -n -e 's/...//p' <<<"${OUTPUT}")
printf '%s\t%s\n' "${OUTPUT}" "$(do_something_with $data_captured)"
This has the unfortunate side effect that it doesn't work for interactive complex\_command's, nor in long-lasting ones.
I thought what I'd do was, I would pretend to be one of those dea— Wait a minute. Wrong noscript.
I thought what I'd do was open up a file denoscriptor for reading and writing, start the complex\_command in the background with a tee that performs the sed and sends its output to the extra file denoscriptor. Then, in the main-line of the noscript, perform reads from that file denoscriptor and process them as needed, also generating output asynchronously, if necessary. Would that look something like this?
exec 3<&
complex_command | tee >(sed -n -e 's/...//p' >&3) &
while read -u 3; do
do_something_with $REPLY
done
Problem is, that's not what that syntax actually does. The first line does not create the file denoscriptor 3 for reading and writing locally, so the 2nd and 3rd lines complain about non-existent file denoscriptor 3. This is an area where my bash-fu is weak.
What am I missing?
https://redd.it/1i2s0ww
@r_bash
So the concept is simple. I have a complex command that generates output to the screen. Within that output is a single piece of data that I want to capture and use later, but not in such a way that it disrupts the flow of output to the screen. If the complex command's not interactive and relatively short, I've found I can do this:
declare OUTPUT=$(complex_command)
declare -i data_captured=$(sed -n -e 's/...//p' <<<"${OUTPUT}")
printf '%s\t%s\n' "${OUTPUT}" "$(do_something_with $data_captured)"
This has the unfortunate side effect that it doesn't work for interactive complex\_command's, nor in long-lasting ones.
I thought what I'd do was, I would pretend to be one of those dea— Wait a minute. Wrong noscript.
I thought what I'd do was open up a file denoscriptor for reading and writing, start the complex\_command in the background with a tee that performs the sed and sends its output to the extra file denoscriptor. Then, in the main-line of the noscript, perform reads from that file denoscriptor and process them as needed, also generating output asynchronously, if necessary. Would that look something like this?
exec 3<&
complex_command | tee >(sed -n -e 's/...//p' >&3) &
while read -u 3; do
do_something_with $REPLY
done
Problem is, that's not what that syntax actually does. The first line does not create the file denoscriptor 3 for reading and writing locally, so the 2nd and 3rd lines complain about non-existent file denoscriptor 3. This is an area where my bash-fu is weak.
What am I missing?
https://redd.it/1i2s0ww
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Contribute to my project with bash noscripts
Hello everyone, I hope you are doing well. I need bash devs to contribute with useful noscripts to my bash customization project. It would be appreciated if you can help me in any way. Feel free to propose changes in the project itself, but my main need is to add into the assets/contrib noscripts action. If you decide to help me and contribute, open a PR and I will approve if the noscript fits the project's purpose.
Here is the link: https://github.com/yorevs/homesetup/tree/master/assets/contrib
Thanks for your help.
Edit: Please add your name/contact if you wish, so people know who created it (actually, create a folder with your name and put the noscript in it).
https://redd.it/1i2ssyh
@r_bash
Hello everyone, I hope you are doing well. I need bash devs to contribute with useful noscripts to my bash customization project. It would be appreciated if you can help me in any way. Feel free to propose changes in the project itself, but my main need is to add into the assets/contrib noscripts action. If you decide to help me and contribute, open a PR and I will approve if the noscript fits the project's purpose.
Here is the link: https://github.com/yorevs/homesetup/tree/master/assets/contrib
Thanks for your help.
Edit: Please add your name/contact if you wish, so people know who created it (actually, create a folder with your name and put the noscript in it).
https://redd.it/1i2ssyh
@r_bash
GitHub
homesetup/assets/contrib at master · yorevs/homesetup
The ultimate Terminal experience! Contribute to yorevs/homesetup development by creating an account on GitHub.
Questions about netcat and ports
Hi there,
I am testing the program netcat and I see something that I do not understand so here I am.
I listen to some ports with :
Assuming nc will listen to tcp by default.
Then I send data into a listened port :
The output :
The question, why is nc responding that the data is received at 127.0.0.1 51404, what is this port ? Same, if I send into port 2070, it will answer at 40630 ? etc..
EDIT : it exits with error code 130
https://redd.it/1i33ysk
@r_bash
Hi there,
I am testing the program netcat and I see something that I do not understand so here I am.
I listen to some ports with :
for j in 20{0..9}{0..5}; do nc -lvn `127.0.0.1` $j & doneAssuming nc will listen to tcp by default.
Then I send data into a listened port :
echo lol | nc `127.0.0.1` 2095The output :
Connection received on `127.0.0.1` 51404lolThe question, why is nc responding that the data is received at 127.0.0.1 51404, what is this port ? Same, if I send into port 2070, it will answer at 40630 ? etc..
EDIT : it exits with error code 130
https://redd.it/1i33ysk
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
what about "case-ignore"?
Hi, why not bash ignore uppercase!
vim or VIM opens vim
ls/LS idem...
exit/EX..
ETC..
I don't know about submission flag maybe was a wrong flag
Regards!
https://redd.it/1i3hwtb
@r_bash
Hi, why not bash ignore uppercase!
vim or VIM opens vim
ls/LS idem...
exit/EX..
ETC..
I don't know about submission flag maybe was a wrong flag
Regards!
https://redd.it/1i3hwtb
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
how to catch status code of killed process by bash noscript
Hi every one, I am working on project, and I faced an a issue, the issue is that I cannot catch the exit code "status code" of process that worked in background, take this program as an example, that exits with 99 if it received a sigint, the code:
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
void bye(){
// exit with code 99 if sigint was received
exit(99);
}
int main(int argc,char** argv){
signal(SIGINT, bye);
while(1){
sleep(1);
}
return 0;
}
then I compiled it using
\`gcc example.c -o byeprogram\`
in the same directory, I have my bash noscript:
set -x
__do_before_wait(){
##some commands
return 0
}
__do_after_trap(){
##some commands
return 0
}
runbg() {
local __start_time __finish_time __run_time
__start_time=$(date +%s.%N)
# Run the command in the background
($@) &
__pid=$!
trap '
kill -2 $__pid
echo $?
__finish_time=$(date +%s.%N)
__run_time=$(echo "$__finish_time - $__start_time" | bc -l)
echo "$__run_time"
__do_after_trap || exit 2
' SIGINT
__do_before_wait || exit 1
wait $__pid
## now if you press ctrl+c, it will execute the commands i wrote in trap
}
out=`runbg /path/to/byeprogram`
my problem is I want to catch or print the code 99, but I cannot, I tried to execute the \`byeprogram\` from the terminal, and type ctrl+c, and it return 99, how to catch the 99 status code??
https://redd.it/1i3isxg
@r_bash
Hi every one, I am working on project, and I faced an a issue, the issue is that I cannot catch the exit code "status code" of process that worked in background, take this program as an example, that exits with 99 if it received a sigint, the code:
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
void bye(){
// exit with code 99 if sigint was received
exit(99);
}
int main(int argc,char** argv){
signal(SIGINT, bye);
while(1){
sleep(1);
}
return 0;
}
then I compiled it using
\`gcc example.c -o byeprogram\`
in the same directory, I have my bash noscript:
set -x
__do_before_wait(){
##some commands
return 0
}
__do_after_trap(){
##some commands
return 0
}
runbg() {
local __start_time __finish_time __run_time
__start_time=$(date +%s.%N)
# Run the command in the background
($@) &
__pid=$!
trap '
kill -2 $__pid
echo $?
__finish_time=$(date +%s.%N)
__run_time=$(echo "$__finish_time - $__start_time" | bc -l)
echo "$__run_time"
__do_after_trap || exit 2
' SIGINT
__do_before_wait || exit 1
wait $__pid
## now if you press ctrl+c, it will execute the commands i wrote in trap
}
out=`runbg /path/to/byeprogram`
my problem is I want to catch or print the code 99, but I cannot, I tried to execute the \`byeprogram\` from the terminal, and type ctrl+c, and it return 99, how to catch the 99 status code??
https://redd.it/1i3isxg
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Give a markdown file and create files based on that: Is this possible?
Filename.java
Filename2.java
Filename3.java
The file looks like this. I know chatgpt can do this but I really want myself to learn bash the last time.
- Read the file with read -a command
- when encountered a first-name keep it as touch Filename.java
- Then everything between three backticks are echoed inside that filename.java
And so on.. Do this till you reach last of file.
https://redd.it/1i43g9m
@r_bash
Filename.java
code goes here
Filename2.java
code goes here
Filename3.java
code goes here
The file looks like this. I know chatgpt can do this but I really want myself to learn bash the last time.
- Read the file with read -a command
- when encountered a first-name keep it as touch Filename.java
- Then everything between three backticks are echoed inside that filename.java
And so on.. Do this till you reach last of file.
https://redd.it/1i43g9m
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
I made a simple note taking in bash noscript that utilizes fzf.
Script Repo
Demo
https://preview.redd.it/pl20rbq8lpde1.png?width=860&format=png&auto=webp&s=105a9506b2af4736f12675926c09aeef25f543dd
https://preview.redd.it/mblhvcq8lpde1.png?width=860&format=png&auto=webp&s=3191b2172a80f1dd84aa69c98975494c60a56363
https://preview.redd.it/eu6dheq8lpde1.png?width=860&format=png&auto=webp&s=82136a650b18015a2e7770c2228130a8c6738e76
https://redd.it/1i42s0q
@r_bash
Script Repo
Demo
https://preview.redd.it/pl20rbq8lpde1.png?width=860&format=png&auto=webp&s=105a9506b2af4736f12675926c09aeef25f543dd
https://preview.redd.it/mblhvcq8lpde1.png?width=860&format=png&auto=webp&s=3191b2172a80f1dd84aa69c98975494c60a56363
https://preview.redd.it/eu6dheq8lpde1.png?width=860&format=png&auto=webp&s=82136a650b18015a2e7770c2228130a8c6738e76
https://redd.it/1i42s0q
@r_bash
GitHub
GitHub - JianZcar/notes-bash: A simple note taking tui bash noscript powered by fzf
A simple note taking tui bash noscript powered by fzf - JianZcar/notes-bash
how to change prompt(+command) just before execution (PS0)
Hi, it is easy to invert the colors of my prompt+command:
https://redd.it/1i3pr9t
@r_bash
Hi, it is easy to invert the colors of my prompt+command:
PS1="\e[7m> "; PS0="\e[27m". I want to achieve this look, but only after hitting enter. Does anyone have an idea how to achieve this?https://redd.it/1i3pr9t
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Recommendations for optimizations to bash alias
I created a simple alias to list contents of a folder. It just makes life easier for me.
It works pretty well, however, it's not instant. Nor is it really "semi instant". If I have a folder of about 30 or so items (mixed between folders, files, symlinks, etc). It takes a good 5-7 seconds to list everything.
So the question becomes, is their a more effecient way of doing this. I threw everything inside the function so it is easier to read, so it needs cleaned.
Initially I was using sed for replacements, I read online that awk is faster, and I had originally used multiple steps to replace. Once I switched to awk, I added all the replacements to a single command, hoping to speed it up.
And originally, I was using a single stat command, and using all of the flags, but then if you had files of different lengths, then it started to look like jenga, with the columns mis-aligned. That's when I broke it up into different calls, that way I could format it with printf.
Originally it was:
So I'm assuming that the most costly action here, is the constant need to re-run stat in order to grab another piece of information.
Any pointers would be great. Hopefully I can get this semi-fast. It seems stupid, but it really helps with seeing my data.
https://redd.it/1i4nou3
@r_bash
I created a simple alias to list contents of a folder. It just makes life easier for me.
alias perms="perms"
function perms
{
END=$'\e[0m'
FUCHSIA2=$'\e[38;5;198m'
GREEN=$'\e[38;5;2m'
GREY2=$'\e[38;5;244m'
for f in *; do
ICON=$(stat -c '%F' $f)
NAME=$(stat -c '%n' $f)
PERMS=$(stat -c '%A %a' $f)
FILESIZE=$(du -sh $f | awk '{ print $1}')
UGROUP=$(stat -c '%U:%G' $f)
ICON=$(awk '{gsub(/symbolic link/,"🔗");gsub(/regular empty file/,"⭕");gsub(/regular file/,"📄");gsub(/directory/,"📁")}1' <<<"$ICON")
printf '%-10s %-50s %-17s %-22s %-30s\n' "${END} ${ICON}" "${GREEN}${NAME}${END}" "${PERMS}" "${GREY2}${FILESIZE}${END}" "${FUCHSIA2}${UGROUP}${END}"
done;
}
It works pretty well, however, it's not instant. Nor is it really "semi instant". If I have a folder of about 30 or so items (mixed between folders, files, symlinks, etc). It takes a good 5-7 seconds to list everything.
So the question becomes, is their a more effecient way of doing this. I threw everything inside the function so it is easier to read, so it needs cleaned.
Initially I was using sed for replacements, I read online that awk is faster, and I had originally used multiple steps to replace. Once I switched to awk, I added all the replacements to a single command, hoping to speed it up.
And originally, I was using a single stat command, and using all of the flags, but then if you had files of different lengths, then it started to look like jenga, with the columns mis-aligned. That's when I broke it up into different calls, that way I could format it with printf.
Originally it was:
file=$(stat -c ' %F %A %a %U:%G %n' $1)
So I'm assuming that the most costly action here, is the constant need to re-run stat in order to grab another piece of information.
Any pointers would be great. Hopefully I can get this semi-fast. It seems stupid, but it really helps with seeing my data.
https://redd.it/1i4nou3
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community