0

I am trying to move various files present in a pendrive into their respective folders on PC. ie ,Music files into music folder,text files into Documents and so on.. I have written a script for that and it is working fine(all files are being moved into their respective folders) when I execute by typing ./cpphone.sh but when I use a udev rule the script not executing correctly (No files are being moved into their folders) however the echo's are being stored into the file testlog.txt. So the script is being executed but files are not being moved.Why is it so?. Script is owned by the root.

My Udev rule is ACTION=="add", RUN+="/lib/udev/cpphone.sh"

My script is

    #!/bin/bash
    echo Hello > /home/ArunReddy/testlog.txt
    exec 1>/dev/null 2>&1  
    DIR=$(date +%d-%m-%y) 

    if [ ! -d /home/ArunReddy/Pictures/"$DIR" ];
    then
        cd /home/ArunReddy/Pictures/
    mkdir $DIR
    fi

    if [ ! -d /home/ArunReddy/Pictures/"$DIR" ];
    then
    cd /home/ArunReddy/Documents/
        mkdir $DIR
    fi

    if [ ! -d /home/ArunReddy/Videos/"$DIR" ];
    then
    cd /home/ArunReddy/Videos/
    mkdir $DIR
    fi

    if [ ! -d /home/ArunReddy/Music/"$DIR" ];
    then
    cd /home/ArunReddy/Music/
    mkdir $DIR
    fi

    cd /run/media/ArunReddy/121C-E137
    mv *.png /home/ArunReddy/Pictures/$DIR/
    echo pngfilecmd executed>/home/ArunReddy/testlog.txt
    mv *.jpg /home/ArunReddy/Pictures/$DIR/
    mv *.bmp /home/ArunReddy/Pictures/$DIR/
    mv *.txt /home/ArunReddy/Documents/$DIR/
    mv *.odt /home/ArunReddy/Documents/$DIR/
    mv *.doc /home/ArunReddy/Documents/$DIR/
    mv *.pptx /home/ArunReddy/Documents/$DIR/
    mv *.pdf /home/ArunReddy/Documents/$DIR/
    mv *.ppt /home/ArunReddy/Documents/$DIR/
    mv *.docx /home/ArunReddy/Documents/$DIR/
    mv *.avi /home/ArunReddy/Videos/$DIR/
    mv *.flv /home/ArunReddy/Videos/$DIR/
    mv *.MP4 /home/ArunReddy/Videos/$DIR/
    mv *.mpeg /home/ArunReddy/Videos/$DIR/
    mv *.MP3 /home/ArunReddy/Music/$DIR/
    exit
12
  • I would guess it's a permissions issue. Please add 2>> /home/ArunReddy/error to each of the mv commands. For example mv *.jpg /home/ArunReddy/Pictures/$DIR/ 2>> /home/ArunReddy/error. That will capture any error messages printed. Have a look at the resulting ~ArunReddy/error` file and edit your question to include the errors.
    – terdon
    Commented Apr 21, 2014 at 12:37
  • What happens if there are no files with the extensions you listed? answer: it's an error. Is this the behavior you're expecting? Commented Apr 21, 2014 at 17:04
  • @gniourf_gniourf It will generate an error if there are no files with the extensions are present but I have hidden all the error messages it generates
    – Arun Reddy
    Commented Apr 22, 2014 at 8:19
  • Remove the exec 1>/dev/null 2>&1 part, enclose your full script (well, starting from the line below the #!/bin/bash) into { ... } &> /home/ArunReddy/error. This will log the stdout and stderr of your script into the file /home/ArunReddy/error so that you can analyze the errors. Commented Apr 22, 2014 at 9:25
  • @gniourf_gniourf It shows that the directory for my pendrive is invalid
    – Arun Reddy
    Commented Apr 22, 2014 at 13:27

1 Answer 1

0

Maybe the udev environment that executes those scripts has no PATH defined. Try to use full paths to cd, mkdir, cp etc. commands. You can find out the paths using for example with which cd command in shell.

1
  • No,doesn't work :(. But thanks for the help
    – Arun Reddy
    Commented Apr 22, 2014 at 13:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .