r_bash – Telegram
How to substitute in aliases

I'm doing this in my `.bashrc`

alias git-test="echo ${1}##*\/"

What I want to achieve is to get the last part of a url, for example from `https://www.reddit.com/r/bash/submit` get `submit`. But when I run it, it gives me nothing and I dont know why.

https://redd.it/17o8o38
@r_bash
Use or option for files with awk

I want search for a list with awk:-

awk -F '/_' '/pool/ { print $9 }' /var/log/bootstrap.log /var/log/installer/syslog

is there a way to skip to second file if first file does not exits without error.

Thanks in advance/

https://redd.it/17obktx
@r_bash
Can you help me with my bash noscript / variable??

posh_favs=("cert" "atomic" "bubbles" "jonnychipz")

pick_posh="${posh_favs[RANDOM%${#posh_favs[@]}]}";

eval "$(oh-my-posh init bash --config /home/paulie420/.config/posh_themes/cert.omp.json)"

Lol - dunno why I can't paste that all into one inline code; can yall help me with what I'm trying to do... I want oh-my-posh to use a random theme that I like when terminal opens. So, I added my favs to $posh_favs, then I pick a random one with $pick_posh, now I just need to replace the 'cert' section of the eval command to $pick_posh - I'm having a hard time figuring it out, I think because of the quotes that are needed in the eval line...

Also, is this a 'good' way of doing what I'm trying to do??? Any suggestions to better the bash code are appreciated - but I'm mainly trying to replace 'cert' (in the eval line) with $pick_posh.

Thanks, all bash rockstars!

https://redd.it/17oh892
@r_bash
How to use xdotools to move and then maximize a window when I know the PID?

Hi all,

Struggling a bit here. I want to open two processes which show images so that I can automatically go through and validate the images by sight within a noscript.

Info: I have 4 monitors set up in a 2x2 format. I want the first image to show up in the top left monitor, and the second image to show up in the top right monitor.

For simplicity's sake in solving this part, I won't paste the whole noscript, just the current stuff I'm working with:

# Open the two image files and record their PIDs
gwenview "$PricedCard.CardFrontImgFile" &
FrontImgFilePid=$!

gwenview "$PricedCard.CardBackImgFile" &
BackImgFilePid=$!

# Output of xrandr --listmonitors
xrandr --listmonitors
Monitors: 4
0: +*DP-3 1920/527x1080/296+0+1080 DP-3
1: +DP-0 1920/527x1080/296+0+0 DP-0
2: +HDMI-0 1920/527x1080/296+1917+0 HDMI-0
3: +DP-5 1920/527x1080/296+1918+1080 DP-5

So far I've gotten to xdotools, however I don't know how to use this information to move and then maximize the images into each monitor. I also have the unique IDs of each monitor if that helps.

https://redd.it/17oh4wu
@r_bash
Pattern matching with date-like substrings

Is there a simple way to look for date-like patterns in strings, in my case YYYYMMDD?

abcd20230822efgh -> yes

19850102abcd -> yes

ab
123445678cde -> no

My current Implementation is rather rudimentary and I wonder whether there is a more efficient way. First I get rid of everything in the string besides the numbers. Then I pick a 8 element wide window and test it for a valid date. Then I shift the window 1 element further and test again. This is repeated until a valid date is found or the window reaches the end of the string.
It works but it feels pretty clunky.

https://redd.it/17olrsr
@r_bash
Looking for proper path with noscript

I have a noscript which requires sudo. I setup it up to run using a user service / timer, and that part executes fine.

However, as soon as the noscript is ran (as the user), I get thrown:

sudo[389308]: pam_unix(sudo:auth): auth could not identify password for [username]
MyService[389309]: sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper

I assume my noscript is having issues with not having the sudo password.

I've looked online, and people are suggesting creating a .passwd file and sticking the password in there with chmod 400, however, that seems extremely unsecure.

What are my options for being able to safely pass the sudo password to bash using something like cached credentials?

The noscript needs sudo, and I've tried running the noscript as a user service. I also tried a system service globally, but it also has an issue with needing root's password.

​

https://redd.it/17on7ih
@r_bash
Help with bash parser

Hey guys! Well, I'll be direct, I'm developing a game (text adventure) in Shell Bash, I have some good experience with the language but not with programming games. My question is, how can I make a parser with (verb and noun) for my game? It will have around 30 rooms.

What I'm trying to implement is for example (pick up the torch) (drop the sword) and also (go north). Could you give me some light?


I am literally lost. I know that shell noscript is not the best language for this, but it is evolving in the language
I did something like this, would that be the way?

function VERB()

{

\# Only verb here

case verb in

take) NOUN ;;

drop) NOUN ;;

*) echo "$userInput WHY?"

esac

}

while true; do

read -rep "> " verb noun

VERB

done

https://redd.it/17p6uc0
@r_bash
Looking for bash guide

I’m still a newb so if I’m not asking correctly .. then please help.

So I’ve been tinker with fedora LDXE for a bit and I want to customize my terminal ..

I have a few books and have read a few articles .. everything points me to playing with the .bashrc file (don’t worry VM so I’m breaking lots of things purposely)

Anyways I have two questions to start:

First- my terminal works fine .. I can change my preferences in the file menu …but my .bashrc is pretty much blank .. no PS1 section or Color section like some of the tutorials point out - why is that?!

Second - all the books and online bash manual I read really only talk about changing my prompt .. but I see some people getting really intricate with Color’s .. where can I find the information .. or what language do I need to learn for the Color’s .. I can’t find any info on it?

https://redd.it/17phbi5
@r_bash
Error with bash noscript. Integer expression expected.

Does any one know what I am doing wrong? This is the first bash noscript I have ever written. It's for a class. The noscript is supposed to generate random numbers. So when you run the noscript you type how many numbers you want it to generate in the argument. I thought maybe the issue was I needed $ in front of count and it may still be, but when I tried adding it in, then the noscript wouldn't run at all.

line 12: [: count: integer expression expected

1 numGen=$1 #number of numbers being generated
2 min=$2 #minimum number
3 max=$3 #maximum number
4 average=0 #average of the numbers generated
5 smallest=32768 #smallest number generated
6 largest=0 #largest number generated
7
8
9 if $# -eq 1
10 then
11 count=0
12 while count -lt $numGen
13 do
14 randNum=$RANDOM
15 echo $randNum >> randomNumbers$numGen.txt
16 average=$(($average + $randNum))
17
18 if $randNum -gt $largest
19 then
20 largest=$randNum
21 fi
22
23 if $randNum -lt $smallest
24 then
25 smallest=$randNum
26 fi
27 done


​

https://redd.it/17pi4cq
@r_bash
Improving my bash?

I've finished a 20 hr course on udemy on bash. My linux experience is around 3 months. And this is the noscript that I wrote for clearing logs by traversing the directory.


#!/bin/bash

givenpath=/home/techyman/glassfish4/glassfish/domains
excluded
folder="x"
function clearlog() {
cd "$each"
echo "
$PWD"
find . -mtime +"$1" -name "*.log" -exec rm -f {} \;
#echo "$2"
#echo "$1"
}
for each in "$given
path"/; do
if [ "$each" = "$given_path/$excluded_folder" ]; then
echo "$each"
#echo "This is x"
#echo "Do nothing"
continue
else
#echo "this is not x"
#echo "$each"
#case statement goes here
case $each in
a)

echo "This is a"
clearlog 7 "$each"
;;
*b)
echo "This is b"
clear
log 4 "$each"
;;
c)
echo "This is c"
clear_log 5 "$each"
;;
d)
echo "This is d"
clearlog 6 "$each"
;;
*e)
echo "This is e"
clear
log 8 "$each"
;;
esac
fi
done


Please review. Am I doing enough?

https://redd.it/17pxpn1
@r_bash
grep with perl regex should NOT highlight/match the comma, but it does

Hello people,

​

I have the following code, that should highlight any non-ascii characters in my german txt files (so that i can easy replace/remove it in a next step). That works with one exception:, it also highlights all the commas ',' and i don't know how to get rid of that. The code [1\]:

grep --color='auto' -P -n "[\\x80-\\x81,\\x83-\\xC3,\\xC5-\\xD5,\\xD7-\\xDB,\\xDD-\\xE3,\\xE5-\\xF5,\\xF7-\\xFB,\\xFD-\\xFF\]" inputfile.txt

Output lists lines that contains comma ',' but that is not wanted. Does anybody see the mistake ? I am looking at the example now for 3 hours but i can't get it.

​

[1\] (original code source): https://stackoverflow.com/questions/3001177/how-do-i-grep-for-all-non-ascii-characters

Greets

muh

https://redd.it/17q6y9n
@r_bash
Is Bash more powerful than Powershell ? and How good is your Bash compared to Powershell or vice versa ?

Is Bash more powerful than Powershell ? and How good is your Bash compared to Powershell or vice versa ? please don't start to include python in your analysis of bash vs powershell because if you include a programming language like python we have to include C# with powershell etc So what's your opinion of Bash do you feel powershell is more powerful and more modern with object oriented nature and pipeline and all the rest around it ? or you feel windows isn't just made to be managed that way compared to a linux text based command and architecture as a whole ?

Do you feel the intricacies of the operating system matters less and less by the day (in the eye of a pure sysadmin view etc not of a cloud engineer/devops) since everything is moving to Infrastructure as code or inside Kubernetest clusters inside the cloud or serverless configurations etc or the future will be just noscripting in Bash/Powershell ? or virtual desktop like MS want to do with Azure for mostly everything

https://redd.it/17r45fa
@r_bash
How can I get a clean output from echo, without hidden characters?

Hi there,
I am working with lua and bash, and trying to set a variable in lua from a bash command, but this is not working. When I set the variable manually works, so my guess is that there are hidden characters (maybe encoding characters).


This is my line:

local vim.g.kubernetes_cluster = vim.fn.system('echo -n "$(kubectl config current-context 2>/dev/null)"')

So the command bash would be


echo -n "$(kubectl config current-context 2>/dev/null)"

Any advice is welcome! Thanks!

https://redd.it/17r7a91
@r_bash
How to find the youngest file in dir1 and then find all files younger than that in dir2, recursively?

Like the noscript says. I am hard-pressed to add more details without also adding confusion.

If the youngest file in dir1 and all its subdirs is from Nov 1 00:00:00, I want to find all files in dir2 (and all its subdirs) which are younger than that.

Is there a quick oneliner which could solve this?

Solutions for finding the youngest file are available. To use the modification date of this file for another search seems to be a lot more tricky, though.

https://redd.it/17rarw1
@r_bash
Grep/Sed from hit until 2nd newline

Hello,

Im fairly new to linux and bash/noscripting in general. Now i need to search through a large text file, and i want to filter out relevant information. So far i've been using: grep with the -A and -B flags, both taking a number and then including the lines Before and After the hit with grep. Now i have the following challenge:


I want to search the text file, and i want to include a few set amount of lines before the hit (-A flag) but i want to include a varying amount of lines after the hit, namely everything until the 2nd newline. How do i go about this?

Example:
Text.txt is the following:
Apple
Banana
Error
Delta

Echo

Romeo
More stuff
BlaBla

=====================

​

I want to execute the command, something like grep -B 1 'Error' ??SomeMoreFlags/Hacks here??

And in return i expect:
Banana
Error
Delta

Echo

​

Is anyone able to help me? I've heard people mention sed/cat/grep/regex matching and i feel this should be possible, but im having trouble finding the solution.

​

​

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