Score:1

Find command works in terminal but not in automatic shell script

td flag

I am writing a script that automatically copies sqlite files from my computer to a USB stick whenever the stick is inserted. It makes a new directory on the USB that takes the hostname of the computer, as I will be downloading from multiple computers.

The script is as follows:

#!/bin/bash
# Set the source folder to download from
src_dir="/home/atlas/atlas/runs/server"

# Define hostname 
usb_hostname=$(cat /etc/hostname)

# Find USB path
usb_path=$(find /media/atlas/*/ -maxdepth 0 -type d -print -quit)
echo "USB path = $usb_path"

# Set the destination 
dest_dir=$usb_path$usb_hostname

# Mount the USB device
sudo mount -t vfat /dev/sd?1 dest_dir

# Create a directory on the USB stick 
sudo mkdir -p $dest_dir

# Copy the files with 'sqlite' in the extension from the source folder to the destination folder
find $src_dir -type f -iname "*sqlite*" -exec cp {} $dest_dir \;

# Unmount the USB device
sudo umount dest_dir

The script works perfectly when I run it in terminal, however when it runs on insertion it fails on the 'find' command (line 'usb_path ='), where it returns 'No such file or directory'.

Things I have tried:

  • Setting 'sleep 5' at the start of the script to allow time to mount; I've extended the time of this too with no change
  • Checking permissions for the script to access the /media/atlas/ directory
  • There are no errors in the syslog file
  • 'atlas' is my user; if I change this to $(whoami) the script changes the path to /media/root/ and also cannot find any directory
  • The USB device is mounted correctly (as I said, the script works fine from terminal)
  • I've checked and the script does recognise /media/atlas, it just doesn't recognise any sub-directories

I'm at my wits end here. I can see that the problem is that when automatically run, the shell script can't find any directories within /media/atlas/ but I have no idea why. Any suggestions would be very much appreciated.

hr flag
I'm confused about what you're trying to do here - if the USB doesn't get automounted under /media/atlas/ then you won't be able to find it with `find` - but if it does get automounted, why would you want to mount it again?
Raffa avatar
jp flag
"*I am writing a script that automatically copies sqlite files from my computer to a USB stick **whenever the stick is inserted***" ... You are probably using a udev rule for that ... If so, then it wont work ... Plus the way you detect the partition on the USB by arbitrarily guessing `/dev/sd?1` (I understand you might have only NVME or SSD so the naming is different) but still this is not the way to go ... See for example [Different behaviour of bash script in udev](https://askubuntu.com/q/1417539)
John Tate avatar
td flag
The mounting lines are a mistake then on my part - as is probably apparent I am very new to this. If I remove the mount commands the script still works in terminal, but not via udev. To clarify, does this mean my aim here is not possible? I would like to set my computer up so that when a USB is inserted, files automatically copy from the computer to the USB. I thought a udev rule was the correct approach - such as was suggested here - https://askubuntu.com/questions/876028/how-to-auto-copy-to-removable-device - but it sounds like this circularity makes this impossible (at least via udev).
Raffa avatar
jp flag
"*To clarify, does this mean my aim here is not possible?*" … Not with a udev rule … See my answer in the post I linked above for how to automatically detect and mount a USB disk partition with a bash script the right way … Example template scripts that you can build on and modify to your needs are included in the answer.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.