r_bash – Telegram
find -exec executing on more files than find lists?

Edit: Of course as soon as I posted, I think I figured out I'm an idiot and that it's executing on the directories and I need to include -type f (or ! -type d would be better?), but would love any input from others anyway on the matter!

Will try to exclude extraneous details, but please let me know if more details would help. Basically, I have a folder structure where some files were changed recently. I'd like to archive just those newer files in a way that preserves the folder structure.

(This is because I already archived and compressed the original files, and figure it's easier just to have two archives side by side to avoid recompressing and reuploading.)

I am trying this out:
find . -ctime -2 -exec bash -c 'tar rvf archive.tar "{}"' \;


When I run find . -ctime -2 | wc -l, I get 978. However, running the above command, it archives over 1030 before I stop it, and it looks like it repeats some files.

Could anyone please help explain why it's not just archiving only the 978 newer files that I intend to archive? Alternatively, any better solution to achieve the same goal?

TIA!

https://redd.it/101l27c
@r_bash
cannot get bash to do case-insensitive completion

$ echo $BASH_VERSION
4.4.12(3)-release

$ shopt|grep case
nocaseglob on
nocasematch on

$ bind 'set completion-ignore-case on'

$ compgen -W 'foo Frank' f
foo

$

This is very frustrating. What is the point of having three separate options to ignore case if none of them work? Just to taunt the user?

How can I get bash to do case-insensitive completion?

https://redd.it/101p4vu
@r_bash
Will this also delete directories or just the files in them?

find /home/daphipz/Downloads/* -atime +7 -exec rm {} \;

This should delete all files in my Download directory that were not accessed in roughly the last 7 days (I dont care about the rounding `atime` does). I also want to delete all subdirectories that match this criteria.

Does `find` also check the directories' `atime` and delete them, or do I need to check for empty folders after executing this line?

https://redd.it/102441z
@r_bash
How to get mouse position in bash

I want to get the mouse cursor position using a bash noscript in a row-col format.

I am making a painting program for the terminal where I want to use the mouse to paint. It is for personal use, but I am reusing the code for other public projects later.

Thanks!

https://redd.it/1026eoh
@r_bash
Saving command with pipes output to variable works in terminal but not on noscript

Context

I have a bash noscript that is executed with cron multiples times a day, the bash noscript calls a python program that does a request to an API and saves the Python output to a variable (this output is a string with spaces).

> var1="$(python $pythonfilepath)"

What I'm interested in doing is saving this output to a log file only if it has not been saved before (this API is updated once daily, but not at the same time every day). So I read the last line of the log file with
> var2="$(tail -1 $logpath)"

And then I compare var2 with var1 and if they are different I save the new value to the file.

**Issue**

There is a weird issue that I can't tell so far if it is on my end or the API, there are some occasions where after updating the value a few minutes later when the noscript is executed again it obtains the value of the day before (some type of cache issue or something like that) so when the noscript compares the value obtained with the last line, they are different, and it saves the old value again, and then a few minutes later it saves the value of that day again.

**My attempt at fixing it**

What I tried to do was changing the second command to
> var2=$(cat $log
path | grep "$var1")

so if the value in var1 is found with grep it outputs that line and the comparison of var1 and var2 can be done and do the rest of the noscript, but this command does not work inside the noscript, it only works on my tests directly on the terminal

https://redd.it/102r751
@r_bash
Help With sed Variable String Manipulation

Hey All!

I am having some trouble manipulating a string contained in a variable using sed and was hoping someone could give me some pointers. I need to replace the `https://` portion of the `ORIGINALURL` variable with `rewrite (?i)^`

The end result being the following: `rewrite (?i)^ example.org/resouces/testing/pages/store`

I want to store the above end result in a separate variable to be called later.

I keep getting stuck on the syntax of sed because I am working with special characters making sed misinterpret things. Any guidance would be appreciated!

ORIGINALURL='https://example.org/resouces/testing/pages/store'
SEDREPLACEMENT='https://example.org'
SEDRAPLACEMENT2='rewrite (?i)^'

EDITEDORIGINALURL=$(echo $ORIGINALURL | sed -e "s\/$SEDREPLACEMENT\/$SEDREPLACEMENT2/g")

When running the `EDITEDORIGINALURL` variable, I receive a `sed: -e expression #1, char 36: unterminated \`s' command` error.

https://redd.it/10365pg
@r_bash
Help with Bash Script - Works from the command line but not via Cron

Greetings, I am having a difficult time with a bash noscript to run daily to backup a database (among other things) The primary issue is that when I test manually, it \*seems\* to work. However, when run from cron, the noscript runs but doesn't do anything. Here is my noscript:


#!/bin/bash

#Definitions
DATE=$(date +%Y%m%d)
DATETIME=$(date +%Y%m%d-%T)
DIR_MONGO="/media/RAID5/graylog/backup/mongodb/"
DIR_CONF="/media/RAID5/graylog/backup/conf/"
DIR_ELASTIC="/media/RAID5/graylog/backup/elasticsearch/"
CLEAN_MONGO="find $DIR_MONGO* -type d -mtime +90 -exec rm -rf {} \;"
CLEAN_CONF="find $DIR_CONF* -type d -mtime +90 -exec rm -rf {} \;"
CLEAN_ELASTIC="find $DIR_ELASTIC* -type d -mtime +90 -exec rm -rf {} \;"
MONGODUMP="/usr/bin/mongodump --quiet -o $DIR_MONGO$DATE"
CONFDUMP="cp /etc/graylog/server/server.conf $DIR_CONF$DATE"
ELASTICDUMP="/usr/local/bin/multielasticdump --direction=dump --input=http://localhost:9200 --output=$DIR_ELASTIC$DATE --type=mappging"
LOG_FILE="/home/ubuntu/graylog/logs/$DATETIME.log"
BODY=$LOG_FILE

# Log Everything
exec 3>&1 4>&2
trap 'exec 2>&4 1>&3' 0 1 2 3
exec 1>$LOG_FILE 2>&1
#Everything below will log to $LOG_FILE


#Create Logfile
if test -f "$LOG_FILE";
then
echo "File Found"
else
touch $LOG_FILE
fi

#Cleanup Mongo
if test -d "$DIR_MONGO";
then
$CLEAN_MONGO
fi

#Cleanup Conf
if test -d "$DIR_CONF";
then
$CLEAN_CONF
fi

#Cleanip Elastic
if test -d "$DIR_ELASTIC";
then
$CLEAN_ELASTIC
fi

#Mongo
if test -d "$DIR_MONGO$DATE";
then
$MONGODUMP
else
mkdir -p "$DIR_MONGO$DATE"
$MONGODUMP
fi

#CONF
if test -d "$DIR_CONF$DATE";
then
$CONFDUMP
else
mkdir -p "$DIR_CONF$DATE"
$CONFDUMP
fi

#Elastic
echo "This is going to take a long, long, long time"
if test -d "$DIR_ELASTIC$DATE";
then
$ELASTICDUMP
else
mkdir -p "$DIR_ELASTIC$DATE"
$CONFDUMP
fi

echo "Done?"

#Send Notice
if test -f "$LOG_FILE";
then
echo "" | mutt -i "$BODY" -s "Graylog Backup Report" -- "recipient@foo.bar"
else
echo "" | mutt -s "ERROR - Graylog Backup Report - No Log File Found" -- "recipient@foo.bar"

So, the first issue (and this may cause subsequent issues) when running the cleanup portion of the noscript I get these errors:

find: missing argument to `-exec'
find: missing argument to `-exec'
find: missing argument to `-exec'

The next issue(s) are that it doesn't actually backup anything.

The log file looks like this:


File Found
find: missing argument to `-exec'
find: missing argument to `-exec'
find: missing argument to `-exec'
This is going to take a long, long, long time
Done?

Which doesn't look like its logging everything but thats a tertiary issue (*N.B.:* *I don't fully understand that portion of the noscript; that is a product of google-foo*)

Thank you kindly for any assistance you can provide :)

https://redd.it/1034il5
@r_bash
Elegant way to add to a string within an associative array?

     I'm using GNU bash, version 5.1.16(1)-release-(x86_64-pc-linux-gnu)x.

     I'm after an elegant way of taking a string element of an associative array, conjoining another string to it, and putting it back into the array. At present I have the following, inelegant code (but I have not managed to find a better method).

strTmp="${job[opts]}"
strTmp="${strTmp} ${rlj_jobs_copy[*]}"
job[opts]=${strTmp}

https://redd.it/103gle7
@r_bash
Something briefly flashes in the taskbar

I'm trying to keep recordings of when microphone exceeds a certain volume. It's running in a terminal in Yakuake

But it's bugging me because on every iteration, something seems to briefly appear in the KDE taskbar which bumps all the taskbar buttons to the side momentarily

It seems to happen as long as I use arecord and wait.

How can I prevent whatever's briefly popping into my taskbar?

#!/bin/bash

# Set the threshold, duration (in seconds), and recording directory
threshold=0.2
duration=10
recording_dir="/mnt/tera/audio/NOISE2"

# Set output options
output_to_console=true
output_to_log=true


# Set trap to handle SIGINT (CTRL+C) signal
trap "echo Exiting...; exit" SIGINT

while :
do
# Record audio for the specified duration
timeout "$duration" arecord -d "$duration" -f S16_LE -c 1 -r 44100 audio.wav &> /dev/null &
pid=$!

# Wait for the arecord command to complete
wait $pid

# Calculate the average audio level
level=$(sox audio.wav -n stat 2>&1 | grep "Maximum amplitude" | awk '{print $3}')

now=$(date +"%Y.%m-%d.%T")

# Save the file and log the timestamp and maximum amplitude
log_entry="$now $level"

if (( $(echo "$level > $threshold" | bc -l) )); then
mv audio.wav "$recording_dir/$now $level.wav"
fi

if $output_to_console; then
echo "$log_entry"
fi

if $output_to_log; then
echo "$log_entry" >> "$recording_dir/audio.log"
fi
done


https://redd.it/103xkt8
@r_bash
Calling variable inside variable assignment

I have a line of code which is :

​



echo "$line"

CURL_REQ=$(curl -is $line -H 'Pragma: akamai-x-get-extracted-values' | grep 'AKA_PM_PROPERTY_NAME')

echo "$line"

echo $CURL_REQ

​

Why does $line in 2nd line of code doesn't work ?

If I replace $line with status domain like myntra.com . It works and echo $CURL_REQ shows the value.

https://redd.it/103yvoa
@r_bash
bash while loop over command output

Hi! i have this command:

aws ec2 describe-snapshot-tier-status --filters "Name=tag:MarkedForDeletion, Values=True" --region us-east-1 --profile default | jq -r '.SnapshotTierStatuses[\].LastTieringOperationStatus' | grep archival-in-progress | wc -l

I don’t remember how to work using the while, I need to run a command over and over again, when the result of the command I mention (aws cli) is less than 25.

​

Any helps?

https://redd.it/104a5rr
@r_bash
Bash noscript help

Hi everyone 😊. Hope you all enjoy your holidays!

I was in the creation of a noscript and I would like to add a small detail in my noscript but I am stuck on how I can do it. Let me show and explain this better.

#!/bin/bash

###########################
# Created by Diego Castro #
###########################
Help()
{
# Display Help
echo "This noscript will run the records for any domains."
echo
echo "Syntax: ./records.sh [-h|d|s|a|m] [-S]"
echo "Options:"
echo "h Prints Help section."
echo "d Argument for DMARC record."
echo "s Argument for SPF record."
echo "a Argument for A record."
echo "m Argument for MX record."
echo "S +short"
echo
}

DMARC(){
read -p "Domain: " dmarc
echo "----------------------------------"
dig txt _dmarc.$dmarc
echo "----------------------------------"
}

SPF(){
read -p "Domain: " spf
echo "----------------------------------"
dig txt $spf | grep -i spf
echo "----------------------------------"
}

ARECORD(){
read -p "Domain: " aRecord
echo "-----------------"
dig A $aRecord
echo "-----------------"
}

MX(){
read -p "Domain: " mx
echo "-----------------"
dig mx $mx
echo "-----------------"
}

while getopts ':hdsamS' OPTION;
do
case "${OPTION}" in
h) #This will display Help
Help
exit
;;
d) #For DMARC
DMARC
exit
;;
s) #For SPF
SPF
exit
;;
a) #For A record
ARECORD
exit
;;
m) #For MX record
MX
exit
;;
S) #SHORT
#Need to know what to do here
exit
;;
esac
done

So in this code I can gives the option of choosing DMARC for example. That would be ./records.sh -d. Everything good until there but I would like to specify the option in the CLI, something like this. ./records.sh -d [facebook.com](https://facebook.com) (or any domain) but I don't see how I can do that.

​

As well, another thing I would like to do is the possibility to add at the end of every result, this option +short. That would be with the +S option but I don't know how I can add it to every place there to get the option.

So, for example, if I run the noscript: ./records.sh -d [facebook.com](https://facebook.com) \-S, that would give this result:

10 smtpin.vvv.facebook.com.

Really appreciate the help and the time to help me guys.

https://redd.it/104rq1j
@r_bash
How to using environment variables in jq replace command

This jq lookup command works using the --arg flag to use an environment variable -

jq --arg newval "$VARIABLE" '.environments.dev.kafkaVersion |= $newval' xena.json

Yet when I try to use the --arg flag in a jq replace command it returns a compile error -

TARGET_ENVIRONMENT="dev"

jq --arg environment "$TARGET_ENVIRONMENT" -r '.environments.$environment.kafkaVersion' xena.json

The same command without using the --arg to use a variable does work:

jq -r '.environments.dev.kafkaVersion' xena.json

What am i doing wrong here?

https://redd.it/104ug7x
@r_bash
suggestions to refine admittedly clumsy scrip

This:

#!/bin/bash

IFS=$'\n'

echo -n "suffix: "
read o in $STDIN

for i in *."$o" ; do exiftool "$i" | grep -i duration > "$i".duration ; done
for d in *.duration ; do sed -s 's/Duration : 0://' -i "$d" ; done
for d in *.duration ; do sed -s 's/ (approx)//' -i "$d" ; done
for f in *."$o" ; do renamexm -s/."$o"/\ \[`cat "$f".duration`\]."$o"/ "$f" ; done
rm -fv *.duration

1) is inelegant, and

2) functions with wav flac ogg mp3 but fails with m4a and mp4 files, truncating the final ]."$o"

3) Is exiftool inaccurate, or am I seeing artefacts of codec litter? I regularly get *very* different durations from a source wav and its compressed children [esp. mp3, but sometimes ogg. flac is usually not off by more than 1s, if at all].

Thanks for any suggestions, pointers, especially those that are *simple* as I am still quite new to this. :)

https://redd.it/10581xl
@r_bash
help prefer to have editable input

I wrote this super simple helper...

#!/bin/bash

IFS=$'\n'

echo -n "artist: "
read a in $STDIN
echo -n "noscript: "
read t in $STDIN
echo -n "year: "
read y in $STDIN

mkdir -p "$a"/"$t""$y"
cd "$a"/"$t"
"$y"

pwd ; echo ready.

... but if I make any mistake typing, backspace just adds "\^H" instead.

How can I fix this?

https://redd.it/1057eua
@r_bash
Radio Streamers Using Rofi and Fzf
https://redd.it/101sdpb
@r_bash
Reclusively touching a file in each directory

I'd like to go down a directory structure and touch a file and add some content inside the file I touch, as such:

filename : desktop.ini

content:
Logo=$currentdirectorypath\1.jpg

Please and thanks

https://redd.it/105j8f0
@r_bash