r_bash – Telegram
What is wrong with my command using rsync?

Hi
using rsync from home to media (a pendrive) I get an error 30

rsync: receiver mkstemp "/media/jazei/MSDB/Vim/.plugins.txt.uul3Lm" failed: Read-only file system (30)

even using dirdiff I get same error read only file system...

what should I check?

I tryed chmod 777and sudo chmod... but nothing I am shielded !

this is a micro sd memory ...

see this URL screen shot: https://imgbox.com/9olj7ivT

Thank you and regards!

https://redd.it/1ixzbe1
@r_bash
I configured my bash to simulate bottom padding so my command prompt is never on the last row
https://redd.it/1iya9vk
@r_bash
Where to put sourced functions?

What is the recommended place to put sourced functions in Bash? What if I want to share those functions with other users?


`.bashrc` is probably the most obvious place, but that doesn't seem to scale very well. Is there maybe some standardized place where I can put them?

https://redd.it/1iypvm8
@r_bash
pgrep shows PID but there's no instance

I'm confused why `pgrep tmux` shows the PID in the noscript even though tmux hasn't been called yet (and I confirmed `pgrep tmux` shows nothing prior to running the noscript):

Script:

#!/usr/bin/bash

if [[ $# -eq 1 ]]; then
selected=$1
else
selected=$(find ~/bin/ ~/work/builds ~/projects -mindepth 1 -maxdepth 1 -type d | fzf)
fi

if [[ -z $selected ]]; then
exit 0
fi

selected_name=$(basename "$selected" | tr . _)
tmux_running=$(pgrep tmux)

echo TMUX=$TMUX
echo tmux_running=$tmux_running
if [[ -z $TMUX ]] && [[ -z $tmux_running ]]; then
tmux new-session -s $selected_name -c $selected
exit 0
fi

if ! tmux has-session -t=$selected_name 2>/dev/null; then
tmux new-session -ds $selected_name -c $selected
fi
tmux switch-client -t $selected_name

My shell:

# same condition as in noscript
$ ~ $ [[ -z $TMUX ]] && [[ -z $tmux_running ]] && echo ok
ok
$ ~ $ tmux kill-server
no server running on /tmp/tmux-1000/default
$ ~ $ pgrep tmux
$ ~ $ tmux-sessionizer
find: ‘/home/enory/work/builds’: No such file or directory
find: ‘/home/enory/projects’: No such file or directory

TMUX=
tmux_running=38971
no current client

Here, there's a PID of `38971` for tmux even though tmux hasn't started yet. As a result, the last command of the noscript runs, resulting in `no current client`.

How is this possible? **What's bizarre is if I run `bash -x tmux-sessionizer`, then the results are expected, i.e. there's no PID yet and the noscript works as intended.**

https://redd.it/1izaz8d
@r_bash
Best way to learn BASH noscripting as a lawyer?

I don’t come from a tech or computer science background—I’m an attorney, and a significant portion of my work revolves around legal documentation. Much of my daily tasks involve repetitive processes, such as OCR (Optical Character Recognition) for scanned documents, formatting files, and managing large volumes of paperwork.

A few days back, I had a monotonous task in front of me: OCRing about 40 PDFs. Under normal circumstances, this would involve opening each document separately or using an online service, which is time-consuming and inefficient. The sheer drudgery of the task led me to wonder if there was an easier way.

That's when I approached ChatGPT for assistance. It recommended writing a Bash noscript to run the task using an ocrmypdf tool. I never wrote a noscript in my life, but I tried it. ChatGPT gave me the noscript, and as soon as I ran it, everything became really simple. Rather than handling every file separately, all I had to do was:

Put all the PDFs in one folder.
Run the noscript.
The noscript automatically produced an output folder and OCR'd all of them simultaneously.
It was an eye-opener experience. I had come to the realization that I could drastically decrease the effort spent manually doing these tasks and have a much more convenient life if I could do some basic Bash noscripting. If I am able to automate a single monotonous task, then likely several others, then hours worth of work can be saved down the road.

Where Should I Start Learning Bash Scripting?
I now understand the value of noscripting, and I would like to learn more and discover how to create my own automation noscripts. As I don't come from a programming background, I'm searching for the best beginner resources where I can start.

Would online video tutorials, books, or articles be the way to go? Do you have any suggestions for certain courses, books, or websites that one can learn Bash noscripting from scratch, and I'd be more than happy to hear them!

https://redd.it/1izlahs
@r_bash
why does rm remove "any-word*.any-ext" plus any-word01.any-ext?

**Hi**, I'd like to know why rm removes screen.jpg plus screen01jpg+screen##.... jpg **when I do rm any-word*.any-ext**?

rm screen*.jpg

*and this command deletes screen.jpg!*

how will be the command for not remove screen.jpg and yes screen01.jpg + screen02.jpg....

Thank you and Regards!

https://redd.it/1izsod6
@r_bash
I used xidel as a web scraper for scraping government job websites of Nepal (they're safe as they never update)

Can anyone recommend anything else? No problem with xidel though. I am on rocky 9

https://redd.it/1j03khv
@r_bash
was this a good case for automation

i had a task today - change old license agreement in 500 files in git repo. The problem was that there was no single template for license agrmt, various devs and teams wrote templates in all kinds of ways - short(4 lines) and long ( 20-50 lines) in several formats

here are templates, even varied by year:

/*

* Copyright (C) 2020 COMPANY NAME

* All Rights Reserved

*/

typical long full license header:

-- /*

-- * NOTICE: All information contained herein is, and remains

-- * the property of company_name and its suppliers,

-- * if any. The intellectual and technical concepts contained

-- * herein are proprietary to company_name and its suppliers

-- * and may be covered by U.S. and Foreign Patents, patents in

-- * process, and are protected by trade secret or copyright law.

-- /*



template 2:

/*

*

*

*

* copyright (c) company name

* All Rights Reserved

*

*

* NOTICE: All information contained herein is, and remains

* the property of company_name and its suppliers,

* if any. The intellectual and technical concepts contained

* bla bla bla

* bla bla bla

* bla bla bla

* from company_name.

*

/*

/*

* company (c) 2019 company name

* all rights reserved

*

*/

/*

* company (c) 2020 company name

* all rights reserved

*

*/

template 3:

/*

*

*

*2020 company_name.

* All Rights Reserved

* Copyright (C)

*

*

* NOTICE: All information contained herein is, and remains

* the property of company_name and its suppliers,

* if any. The intellectual and technical concepts contained

* herein are proprietary to company_name and its suppliers

* and may be covered by U.S. and Foreign Patents, patents in

* process, and are protected by trade secret or copyright law.

* Dissemination of this information or reproduction of this material

* is strictly forbidden unless prior written permission is obtained

* from company_name.

*

*

/

i cloned the repo

asked ai to write prompt to catch the files and remove the text using while loop + sed

and it did not work, then i just manually found all different templates based on "Copyright company_name" or variation of that and removed under 15 min ))


should i have done all the effort to write perfect bash noscript or i did it manually and it was good?

https://redd.it/1j0repw
@r_bash
How can it be both Link and Directory?

/bin/bash --version
GNU bash, version 5.2.37(1)-release (x86_64-pc-linux-gnu)
$ cat test1.sh
#!/bin/bash
mkdir -p dir
ln -sf dir link2dir
test -d dir && echo "dir is Directroy."
test -h link2dir && echo "link2dir is link"
test -d link2dir && echo "link2dir is Directroy"
$ ./test1.sh
dir is Directroy.
link2dir is link
link2dir is Directroy

https://redd.it/1j10hbg
@r_bash
Running a periodic copy noscript. Using cp -n because I don't want recursion. Get error as a result.

I have a noscript running that periodically sweeps a bunch of sftp uploads from branch offices. Each office has a /bleh/sftp/OfficeName/ dir, and an /bleh/sftp/OfficeName/upload/ subdir where files are uploaded to them. I don't need or want those copied back to where I'm processing these other files they've uploaded back to me, so I use the command

cp -n /bleh/sftp/OfficeName/ /opt/crunchfiles/officecode/

Which gives the desired result, omitting the contents of the upload/ subdir. However, I receive the output:

cp: -r not specified, omitting directory '/bleh/sftp/OfficeName/upload'

To which I have taken to replying "NO SHIT! That's what you are supposed to be doing, it's not an error or misconfiguration, it's an intentional use of switches to get the result I want!"

Redirecting the output to /dev/null as in

cp -n /bleh/sftp/OfficeName/
/opt/crunchfiles/officecode/ 2>/dev/null

works to suppress the message, but the noscript still exists with error code 1, which means it still shows up as a failure in my orchestrator. How can I avoid the error code and tell it to just copy the files specified by the switches and stop messing me up with my metrics?

https://redd.it/1iyt79j
@r_bash
FUNCNAME array with some empty values

Hi,

I'd like to print an error message displaying the call stack from a specific function that is passed an error message, the specific function having to display the call stack plus the error message

I thought I could use FUNCNAME array within that function
Strangely though, FUNCNAME array has 20 values, the 16th last being empty ...
Thus I can't use FUNCNAME length to determine the main noscript filename that would be ${BASH_SOURCE[${#FUNCNAME}-1\]} and output the names in FUNCNAME array from first to penultimate value.

Of course, it's possible to get the last index which value is not empty, but I'd like to understand why FUNCNAME lists those empty values.

Thanks for your help !

https://redd.it/1j23zsk
@r_bash
Notifications in the terminal

Hello, I wanted to make a command that would print out desktop notifications in the terminal, by using tail -f on the notification log file.

>tail -f /home/user/.cache/xfce4/notifyd/log | grep -E "app_name|summary|body"

works as intended, i get the response:

>app_name=notify-send

>summary=1234

>body=

>app_name=notify-send

>summary=test2

>body=

But when i add awk, to only print out the content after the equals sign I get no response. The command is running but it's returning nothing.

>tail -f /home/user/.cache/xfce4/notifyd/log | grep -E "app_name|summary|body" | awk -F'=' '{print $2}'

with set -x I get:

>+ tail -f /home/user/.cache/xfce4/notifyd/log

>+ grep -E 'app_name|summary|body'

>+ awk -F= '{print $2}'

I tried making a noscript with a while expression instead of awk, again no output.

>#!/bin/bash

># Path to the log file

>LOG_FILE="/home/user/.cache/xfce4/notifyd/log"

># Tail the log file, filter lines, and extract content after the equals sign

>tail -f "$LOG_FILE" | grep -E "app_name|summary|body" | while IFS='=' read -r key value; do

>echo "$value"

>done

I honestly don't understand where the issue is. Any help is much appreciated.

https://redd.it/1j1z23q
@r_bash
what did I do wrong

 \#!/bin/bash

[ttjz8b@delmar \~\]$ echo Name,phoneNumber,Expense1,Expense2,Expense3 > move1.csv

[ttjz8b@delmar \~\]$ cat data.csv >> move1.csv

[ttjz8b@delmar \~\]$ grep "\^S.*r," data.csv | sed 's/\^\\([\^,\]*\\),\\([\^,\]*\\),.*/\\1,\\2/' > move2.txt

[ttjz8b@delmar \~\]$ grep "(777)" data.csv | sed 's/.* \\([\^,\]*\\),.*/\\1/' > move3.txt

[ttjz8b@delmar \~\]$ grep ",.* [ES\]" data.csv | sed 's/\\([\^ \]*\\) .*/\\1/' > move4.txt

[ttjz8b@delmar \~\]$ grep ',\\$[0-9\]\\{2,\\}\\.[0-9\]\\{2\\},' data.csv | sort -t ',' -k 1.2 | sed 's/\^\\([\^,\]*\\),\\([\^,\]*\\),.*/\\1,\\2/' > move5.txt

[ttjz8b@delmar \~\]$ grep ',\\$[0-9\]*\\.[0-9\]\\{2\\},.*,\\$[0-9\]\\{2,\\}\\.[0-9\]\\{2\\},' data.csv | sort -t ',' -k 1.2 | sed 's/\^\\([\^,\]*\\),\\([\^,\]*\\),.*/\\1,\\2/' >> move5.txt

[ttjz8b@delmar \~\]$ grep ',\\$[0-9\]*\\.[0-9\]\\{2\\},.*,.*,\\$[0-9\]\\{2,\\}\\.[0-9\]\\{2\\}$' data.csv | sort -t ',' -k 1.2 | sed 's/\^\\([\^,\]*\\),\\([\^,\]*\\),.*/\\1,\\2/' >> move5.txt

[ttjz8b@delmar \~\]$ sort -u move5.txt -o move5.txt

[ttjz8b@delmar \~\]$ tr '[:lower:\]' '[:upper:\]' < data.csv | grep '\^A[A-Z\]* ,' | sed 's/,.*//' > move6.txt

[ttjz8b@delmar \~\]$ 

[ttjz8b@delmar \~\]$ tr '[:lower:\]' '[:upper:\]' < data.csv | grep '\^E[A-Z\]* ,' | sed 's/,.*//' >> move6.txt

[ttjz8b@delmar \~\]$ tr '[:lower:\]' '[:upper:\]' < data.csv | grep '\^I[A-Z\]* ,' | sed 's/,.*//' >> move6.txt

[ttjz8b@delmar \~\]$ tr '[:lower:\]' '[:upper:\]' < data.csv | grep '\^O[A-Z\]* ,' | sed 's/,.*//' >> move6.txt

[ttjz8b@delmar \~\]$ tr '[:lower:\]' '[:upper:\]' < data.csv| grep '\^U[A-Z\]* ,' | sed 's/,.*//' >> move6.txt

[ttjz8b@delmar \~\]$ sort -u move6.txt -o move6.txt

[ttjz8b@delmar \~\]$ tail -n +2 move1.csv | perl -F',' -lane '($a,$b,$c)=map{s/\\$//;$_}@F[2,3,4\]; $max = $a>$b ? ($a>$c?$a:$c) : ($b>$c?$b:$c); printf "%s,\\$%.2f\\n", $F[0\], $max;' > move7.txt

[ttjz8b@delmar \~\]$ chmod +x transform.sh

[ttjz8b@delmar \~\]$ /transform.sh data.csv

\-bash: /transform.sh: No such file or directory

[ttjz8b@delmar \~\]$ ls

1         info.shmove2.txt  move5.txt  move.txt       output

data.csv  move1.csv  move3.txt  move6.txt  multiline.txt  transform.sh

file.csv  move1.cv   move4.txt  move7.txt  number         where

[ttjz8b@delmar \~\]$ /transform.sh data.csv

\-bash: /transform.sh: No such file or directory. what did I do wrong

https://redd.it/1j1slc4
@r_bash
Variable populated by docker command acts odd

I would just like to understand why this occurs.

I am running a command on docker containers to get their network id:

iflink=$(docker exec -it "$container" bash -c 'cat /sys/class/net/eth0/iflink')


If I print out the var iflink, it is indeed populated, but it appears that if you try to print anything AFTER the variable, it strips characters after, and the order is weird.

As an example:

iflink=$(docker exec -it "$container" bash -c 'cat /sys/class/net/eth0/iflink')
echo "BEFORE........-${iflink}-...........AFTER"


You get this result:

# Expected
BEFORE........-1874-...........AFTER
BEFORE........-131876-...........AFTER

# Results
-...........AFTER1874
-...........AFTER^X131876


The way I found to solve this was to strip whatever is non-numerical

iflink=$(echo $iflink | sed 's/[^0-9]*//g' )


It works, but I'd still like to know what the heck is going on with the output. It should just be a numerical value

https://redd.it/1j2uykt
@r_bash
Bash not substituting variables on TAB, Macos; does on Debian

Hi!

I'm running Bash 5.2.37 on both my Debian box and my work's laptop, a Mac.

If I try to expand a variable value using Tab when using commands such as ls, Macos doesn't let me but Debian does. Whenever I try to expand a Bash variable by pressing Tab in ls $HO, I get a bell on Macos but I can do it on Debian; the expected behaviour is that I either get the expansion of $HOME (literally, with the $), or a list of suggestion with all of the variables that begin with that string. I have observed that this happens also with cp and mv, but not with user-defined functions or Macos commands such as open.

There are no completion files for the above commands in any of the computers.

Running shopt | awk '$2 == on {print $1}' on both machines returns the same activated options:

autocd
checkwinsize
cmdhist
complete_fullquote
direxpand
expand_aliases
extglob
extquote
force_fignore
globasciiranges
globskipdots
interactive_comments
patsub_replacement
progcomp
promptvars
sourcepath

(Not exactly true; the login_shell option is enabled on Macos for virtual terminals)

How can I solve this? My main reason of exporting variables is to autocomplete them when needed.

Thanks!

https://redd.it/1j2un1v
@r_bash
Named coprocesses, additional ones aren't enumerating their file denoscriptors.

So, I have my docker container in which I want to run my native binaries. Let's say I have

declare -r PROJECT='widget'
declare -ra MOCKS=(gadget what-sit) # thing-ama-jig)

in a file called project.sh, and when another noscriptlet called session.sh where I source that in, and then I loop over MOCKS to launch all of the programs:

for mock in ${MOCK@}; do
echo "Executing coprocess '${PROJECT}-${mock}/cmake-build-mock/${PROJECT}-${mock}.elf ${@}' as ${mock//[:punct:]/}"
coproc "${mock//[[:punct:]]/
}" { ${PROJECT}-${mock}/cmake-build-mock/${PROJECT}-${mock}.elf "${@}"; }
# Plug co-process "${mock//[:punct:]/}" into the command-line interface.
done

And for the first mock program, `gadget`, it's working great. All of my mock programs are sitting out there, pretty as you please, I just need to get them all executing as named coprocs and then I can work on stitching together all of the file denoscriptors into something that makes sense.

$ .
session.sh can0
Executing coprocess 'widget-gadget/cmake-build-mock/widget-gadget.elf can0' as gadget
Executing coprocess 'widget-what-sit/cmake-build-mock/widget-what-sit.elf can0' as what
sit
bash: warning: executecoproc: coproc [4:gadget] still exists
session.sh: line 14: widget-gadget/cmake-build-mock/widget-gadget.elf: cannot execute: required file not found

Well, it was working for the first mock program. Now, it's not. I was able to confirm the coprocesses were running with:

$ ps aux
...
root 6 99.5 0.0 3376 1720 pts/0 R 20:59 6:37 widget-gadget/cmake-build-mock/widget-gadget.elf can0
root 8 99.5 0.0 3376 1720 pts/0 R 20:59 6:37 widget-what-sit/cmake-build-mock/widget-what-sit.elf can0

I was also able to see the gadget file desciptors with:

declare -p gadget
declare -ar gadget=([0]="61" [1]="56)

So, the `gadget` coproc's `stdin` is hiding behind filedenoscriptor 56 and it's `stdout` is hiding behind file denoscriptor 61. I was able to confirm that by sending the `exit` command to the `gadget`'s `stdin` with:

echo exit >${gadget[1]}

and the `widget-gadget.elf` process noped out, as it should.

But then, I couldn't do the same thing with the `widget-what-sit.elf` process. Because some of my mock program names have punctuation in them that aren't conducive to shell symbol names. That's why I use the syntax `"${mock//[[:punct:]]/
}", so all of the punctuation marks will become underscores, which are valid symbol name characters. That makes the widget-what-sit.elf's coprocess name into whatsit`. But if I try to list what-sit's file denoscriptors:

$ declare -p what
sit
bash: declare: whatsit: not found

It's not there. And I'm eventually going to add the thing-ama-jig mock program to the bouquet as well, so I need to be able to access those file denoscriptors for all of my mock processes.

The warning:

bash: warning: execute
coproc: coproc 4:gadget still exists

is apparently from when it launches the widget-what-sit.elf coprocess and the gadget coprocess still exists at that moment, and so I guess it's not creating the what_sit file denoscriptor array. But, it's supposed to!

What's going wrong? What am I missing?

https://redd.it/1j2ttcv
@r_bash
What in god's name is the purpose of this?
https://redd.it/1j2tbdq
@r_bash
Is there a limitation of sorts when using bash and SFTP?

This is what i have so far, and im a total novice here.


Bash
#!/bin/bash

# Configuration
REMOTE_HOST="your-ec2-hostname-or-ip"
REMOTE_USER="ec2-user" # Change to your EC2 username (e.g., ec2-user, ubuntu)
REMOTE_DIR="/app/test123"
LOCAL_DIR=“/app/test/app/backend/xeg"
KEY_PATH="$HOME/.ssh/id_rsa" # Path to your SSH private key
EMAIL_TO="your-email@example.com,colleague@example.com" # Comma-separated list of email recipients

# Use a directly created email content variable
EMAIL_CONTENT=""

# Function to log messages to both console and email variable
log_message() {
local message="$1"
local timestamp="$(date '+%Y-%m-%d %H:%M:%S')"
echo "${timestamp} - ${message}"
EMAIL_CONTENT="${EMAIL_CONTENT}${timestamp} - ${message}
"
}

# Start logging
log_message "Starting SFTP sync process"
log_message "Attempting to connect to $REMOTE_HOST as $REMOTE_USER"

# Create local directory if it doesn't exist
if ! mkdir -p "$LOCAL_DIR" 2>/dev/null; then
log_message "ERROR: Cannot create local directory at $LOCAL_DIR"
echo "$EMAIL_CONTENT" | mail -s "[ERROR] SFTP Transfer - Directory Creation Failed" "${EMAIL_TO}"
exit 1
fi
log_message "Local directory confirmed at $LOCAL_DIR"

# Check if SSH key exists
if [ ! -f "$KEY_PATH" ]; then
log_message "ERROR: SSH key not found at $KEY_PATH"
echo "$EMAIL_CONTENT" | mail -s "[ERROR] SFTP Transfer - SSH Key Not Found" "${EMAIL_TO}"
exit 1
fi
log_message "SSH key found at $KEY_PATH"

# Create a temporary SFTP batch file
BATCH_FILE=$(mktemp 2>/dev/null)
if [ $? -ne 0 ]; then
# If mktemp fails, create batch file in current directory
BATCH_FILE="./sftp_batch_$(date +%s).txt"
touch "$BATCH_FILE" 2>/dev/null
if [ $? -ne 0 ]; then
log_message "ERROR: Cannot create temporary batch file"
echo "$EMAIL_CONTENT" | mail -s "[ERROR] SFTP Transfer - Temp File Creation Failed" "${EMAIL_TO}"
exit 1
fi
fi

cat > "$BATCH_FILE" << EOF
cd $REMOTE_DIR
lcd $LOCAL_DIR
ls -la
mget *
bye
EOF
log_message "Created SFTP batch file at $BATCH_FILE"

# Execute the SFTP command with the batch file
log_message "Executing SFTP commands"
SFTP_OUTPUT=$(sftp -i "$KEY_PATH" -b "$BATCH_FILE" "${REMOTE_USER}@${REMOTE_HOST}" 2>&1)
SFTP_STATUS=$?

# Add SFTP output to email content
log_message "--- SFTP Output ---"
EMAIL_CONTENT="${EMAIL_CONTENT}${SFTP_OUTPUT}
"
log_message "-------------------"

# Check if SFTP was successful
if [ $SFTP_STATUS -eq 0 ]; then
log_message "SFTP connection and file transfer successful"

# Check if remote directory exists by looking for errors in the output
if echo "$SFTP_OUTPUT" | grep -q "No such file or directory\|not found"; then
log_message "WARNING: Remote directory may not exist or is empty"
fi

# Count and list files transferred
if [ -d "$LOCAL_DIR" ]; then
# Capture list of files before and after transfer with detailed logging
log_message "DEBUG: Taking snapshot of files before transfer"
BEFORE_FILES=$(find "$LOCAL_DIR" -type f -name "*" 2>/dev/null | sort)
BEFORE_COUNT=$(echo "$BEFORE_FILES" | wc -l)
log_message "DEBUG: Files before transfer: $BEFORE_COUNT"

# Add some files to the debug log
if [ $BEFORE_COUNT -gt 0 ]; then
SAMPLE_BEFORE=$(echo "$BEFORE_FILES" | head -n 5)
log_message "DEBUG: Sample files before:
$SAMPLE_BEFORE"
fi

# Extract information about transferred files from SFTP output
log_message "DEBUG: Raw SFTP output:"
EMAIL_CONTENT="${EMAIL_CONTENT}DEBUG: Raw SFTP Output:
$SFTP_OUTPUT
"

TRANSFERRED_FILES=$(echo "$SFTP_OUTPUT" | grep -E "^Fetching" | awk '{print $2}')
log_message "DEBUG: Files reported as transferred by SFTP: $(echo "$TRANSFERRED_FILES" | wc -w)"

# Sleep briefly to ensure file operations are complete
sleep 2

# Capture list of files after transfer
log_message "DEBUG: Taking snapshot of
files after transfer"
AFTER_FILES=$(find "$LOCAL_DIR" -type f -name "*" 2>/dev/null | sort)
AFTER_COUNT=$(echo "$AFTER_FILES" | wc -l)
log_message "DEBUG: Files after transfer: $AFTER_COUNT"

# Determine if new files were transferred by comparing counts
if [ $AFTER_COUNT -gt $BEFORE_COUNT ]; then
NEW_FILES_COUNT=$((AFTER_COUNT - BEFORE_COUNT))
log_message "Transferred $NEW_FILES_COUNT new files to $LOCAL_DIR"

# Identify which files are new by comparing before and after
NEW_FILES=$(comm -13 <(echo "$BEFORE_FILES") <(echo "$AFTER_FILES"))
log_message "DEBUG: Newly detected files:"
EMAIL_CONTENT="${EMAIL_CONTENT}DEBUG: Newly detected files:
$NEW_FILES
"

# List the transferred files if any
if [ -n "$TRANSFERRED_FILES" ]; then
log_message "Files transferred:"
EMAIL_CONTENT="${EMAIL_CONTENT}--- Files Transferred ---
$TRANSFERRED_FILES
"
fi

# Success subject with timestamp
EMAIL_SUBJECT="[SFTP-SYNC] Transfer Complete - $(date '+%Y-%m-%d %H:%M') - $NEW_FILES_COUNT Files"
else
log_message "No new files detected, nothing transferred"
# Info subject with timestamp
EMAIL_SUBJECT="[SFTP-SYNC] Completed - $(date '+%Y-%m-%d %H:%M') - No Changes"

# Additional debugging if no files detected
log_message "DEBUG: Checking if files exist on remote server"
REMOTE_LS=$(echo "$SFTP_OUTPUT" | grep -A 100 "sftp>" | grep -v "sftp>")
EMAIL_CONTENT="${EMAIL_CONTENT}DEBUG: Remote directory listing:
$REMOTE_LS
"
fi

# Get list of all files in destination
log_message "Total files in destination directory: $AFTER_COUNT"
FILE_LIST=$(ls -la "$LOCAL_DIR" 2>/dev/null || echo "Cannot list directory contents")
EMAIL_CONTENT="${EMAIL_CONTENT}--- Directory Contents ---
${FILE_LIST}
"
else
log_message "WARNING: Local directory no longer exists after transfer"
fi

# Email subject for success
EMAIL_SUBJECT="[SUCCESS] SFTP File Transfer Complete"
else
log_message "ERROR: SFTP connection or file transfer failed with status $SFTP_STATUS"

# Email subject for failure
EMAIL_SUBJECT="[FAILED] SFTP File Transfer Error"
fi

# Clean up the temporary batch file
rm -f "$BATCH_FILE" 2>/dev/null
log_message "SFTP sync process completed"

# Create a temporary file for the email content
EMAIL_FILE=$(mktemp)
if [ $? -eq 0 ]; then
# Write email content to file
echo "$EMAIL_CONTENT" > "$EMAIL_FILE"

# Send email with content-type explicitly set to text/plain
(
echo "To: ${EMAIL_TO}"
echo "Subject: ${EMAIL_SUBJECT}"
echo "MIME-Version: 1.0"
echo "Content-Type: text/plain; charset=utf-8"
echo "Content-Transfer-Encoding: 8bit"
echo ""
cat "$EMAIL_FILE"
) | /usr/sbin/sendmail -t

# Remove temporary email file
rm -f "$EMAIL_FILE"
else
# Fallback to regular mail if temp file creation fails
echo "$EMAIL_CONTENT" | mail -s "${EMAIL_SUBJECT}" "${EMAIL_TO}"
fi

# Output completion message to console
echo "SFTP sync completed. Notification sent to ${EMAIL_TO}"

Apologies for the formatting ahead of time. I dont usually post, if someone could tell me what shortcut to use to format it properly would be appreciated.

Code is garbage, I’m sure, but i am not an expert by any means - Essentally what i am trying to accomplish, is to pull down files from a remote server via sftp. Running this noscript, it does it, but for some reason , when i try to add logic for the notifications, via email to update up when there are no changes, email subject should read - no changes - but whenever i add a file, it shows it and still runs a. The batch job process and pulls the file, but it keeps spitting out “changes” no matter if there is a change..e.x a file added…I can’t
tell if its my shitty code or something which is an underlying issue - open to feedback :)

https://redd.it/1j3ncpj
@r_bash
A process I'm trying to pipe data into using a named pipe reports a "Read error"

Hello everyone!! So I've been trying for the last hour and a half to get the "fftest" process to receive the input "0", so it would run its part of the program. It used to work, but after a couple of attempts and deleting the named pipe file it just stopped working.

The problematic code is this:

#!/bin/bash

loop(){
while [ 1 ]
do
sleep 2
echo 0 > pipef
sleep 18
done
}

mkfifo pipef
cat > pipef &
mypid=$!

trap "pkill -f fftest" SIGINT

loop &
looppid=$!
fftest /dev/input/by-id/usb-MediaTekInc.XBOXACC000000000-event-joystick < pipef

kill $mypid
kill $looppid
rm pipef

I'm creating the loop function that's responsible for the data input, then I open the pipe, then I run the loop, so it would pipe its data when the time comes and then I run the "fftest" command itself. The moment that command is ran it exits, reporting a "Read error".

This code used to work before, until it randomly stopped (that's why I created the new one in an attempt to fix it):

#!/bin/bash

mkfifo pipef
cat > pipef &
mypid=$!

fftest /dev/input/by-id/usb-MediaTekInc.XBOXACC000000000-event-joystick < pipef &
sleep 2
while [ 1 ]
do
echo 0 > pipef
sleep 20
done

kill $mypid
rm pipef

If you have found my mistake, please tell me!! Thank you so so much in advance!!! <3

Edit: This is the output with set -x:

+ mkfifo pipef
+ mypid=31874
+ trap 'pkill -f fftest' SIGINT
+ cat
+ looppid=31875
+ fftest /dev/input/by-id/usb-MediaTekInc.XBOXACC000000000-event-joystick
+ loop
+ [ -n 1 ]
+ sleep 2
Force feedback test program.
HOLD FIRMLY YOUR WHEEL OR JOYSTICK TO PREVENT DAMAGES

Device /dev/input/by-id/usb-MediaTekInc.XBOXACC000000000-event-joystick opened
Features:
Absolute axes: X, Y, Z, RX, RY, RZ, Hat 0 X, Hat 0 Y,
[3F 00 03 00 00 00 00 00 ]
Relative axes:
00 00
Force feedback effects types: Periodic, Rumble, Gain,
Force feedback periodic effects: Square, Triangle, Sine,
[00 00 00 00 00 00 00 00 00 00 03 07 01 00 00 00 ]
Number of simultaneous effects: 16

Setting master gain to 75% ... OK
Uploading effect #0 (Periodic sinusoidal) ... OK (id 0)
Uploading effect #1 (Constant) ... Error: Invalid argument
Uploading effect #2 (Spring) ... Error: Invalid argument
Uploading effect #3 (Damper) ... Error: Invalid argument
Uploading effect #4 (Strong rumble, with heavy motor) ... OK (id 1)
Uploading effect #5 (Weak rumble, with light motor) ... OK (id 2)
Enter effect number, -1 to exit
Read error
Stopping effects
+ kill 31874
./start.sh: line 24: kill: (31874) - No such process
+ kill 31875
+ rm pipef

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