You are not logged in.

#1 2023-10-21 19:59:52

anicearchnewbie
Member
Registered: 2023-05-04
Posts: 14

wl-copy command not working when executed inside my script

i made a small script to easily copy any new files added to a particular directory, the script iterates through the directory and any subdirectories, and it copies any newly added txt files into the clipboard so that i can paste them, but it's not working, i added parts where the script logs its actions, and it seems that everything is working well except the command where wl-copy is executed, here is the script :

#!/bin/bash

LOG_FILE="/home/username/directory/log.txt"
MONITOR_DIR="/home/username/directory/files/"
PROCESSED_FILES="/home/directory/names.txt"

# Create processed_files.txt if it doesn't exist
touch "$PROCESSED_FILES"
touch "$LOG_FILE"
log() {
    echo "$(date +"%Y-%m-%d %H:%M:%S") - $1" >> "$LOG_FILE"
}

process_text_file() {
    local file="$1"
    local filename=$(basename "$file")

    if ! grep -q "$filename" "$PROCESSED_FILES"; then
        cat $file | wl-copy # this line is not doing what it's supposed to 
        cat $file >> $MONITOR_DIR/test.txt
        echo "$filename" >> "$PROCESSED_FILES"
        echo $file >> "$PROCESSED_FILES"
        log "Copied content from $file to clipboard"
    else
      log "File with name $filename already exists"
    fi
}


while true; do
    inotifywait -qq -e create -r "$MONITOR_DIR"

    find "$MONITOR_DIR" -type f -print0 | while IFS= read -r -d '' file; do
        process_text_file "$file"
    done

    sleep 1
done

the command wl-copy is working fine when i execute it in the clipboard, and the cat command also is working fine, but they're not working in the script for some reason. i added sample directory names in the script above, but replaced with the actual directory paths in my script. Any help would be appreaciated.

Last edited by anicearchnewbie (2023-10-21 20:04:00)

Offline

#2 2023-10-22 06:37:59

solskog
Member
Registered: 2020-09-05
Posts: 426

Re: wl-copy command not working when executed inside my script

but they're not working in the script for some reason

What was not working? It's working for me with Wayland/River Window Manager

# echo a > /var/tmp/files/a
# wl-paste
a
# echo b > /var/tmp/files/b
# wl-paste
a
b

Last edited by solskog (2023-10-22 06:38:30)

Offline

#3 2023-10-22 07:11:43

dogknowsnx
Member
Registered: 2021-04-12
Posts: 648

Re: wl-copy command not working when executed inside my script

Also, right now there's one issue I see with your locations:
Line #4:

MONITOR_DIR="/home/username/directory/files/"

should be

MONITOR_DIR="/home/username/directory/files"

because in line #20 you use:

cat $file >> $MONITOR_DIR/test.txt

Did you try

bash -x yourscript

in order to see what's being executed?

EDIT: also, maybe let grep be verbose while troubleshooting
EDIT#2: I just tested your script with the change mentioned above, and file contents are being copied to the clipboard as expected.

EDIT#3: @solskog I guess I never even used a path like that - learned something new, thanks smile

Last edited by dogknowsnx (2023-10-22 08:43:58)


RI - Rest your Eyes and Self

"We are eternal, all this pain is an illusion" - Maynard James Keenan

Offline

#4 2023-10-22 08:35:34

solskog
Member
Registered: 2020-09-05
Posts: 426

Re: wl-copy command not working when executed inside my script

Both works, doesn't matter.

/home/username/directory/files/test.txt
/home/username/directory/files//test.txt

Little optimization.

for file in $(find "$MONITOR_DIR" -type f);do
        process_text_file "$file"
done

Last edited by solskog (2023-10-22 08:36:58)

Offline

Board footer

Powered by FluxBB