Results 1 to 6 of 6

Thread: little shell script

  1. #1
    Join Date
    Jun 2011
    Location
    germany
    Beans
    6
    Distro
    Ubuntu 11.04 Natty Narwhal

    Question little shell script

    hi guys,

    I want a little *.sh -script which move files from the same category to the same folder.

    I tryed:

    Code:
    #!/bin/bash
    blau="\033[1;34m"
    end="\033[0m"
    
     echo "$blau --$end" "$blau Start... $end"
    
    
    mv -v *.avi *.AVI *.mpeg *.MPEG *.divx *.DIVX *.mkv *.MKV *.mpg *.MPG *.mp4 *.MP4 /home/d3z1v/Videos/
    mv -v *.jpg *.JPG *.jpeg *.JPEG *.bmp *.BMP *.png *.PNG *.gif *.GIF /home/d3z1v/Pictures/
    mv -v *.txt *.doc *.docx *.ppt *.pps *.TXT *.DOC *.DOCX *.PPT *.PPS /home/d3z1v/Documents/
    mv -v *.mp3 *.MP3 *.wav *.WAV /home/d3z1v/Music/
    
    
     echo "$blau --$end" "$blau End... $end"
    it works fine, but i dont want to see the errors:
    mv: cannot stat `*.***': No such file or directory

    but i want to see, if the scripts is moving a file//

    it would be even better, if someone could give me the syntax for editing my script that im able to get the messages via:

    Code:
     echo "$blau --$end" "$blau FILENAME was found and moved to Videos... $end"
    Iam using:
    Distro: Ubuntu 11.04 natty
    Environment: Gnome
    Terminal: Gnome-Terminal && fish

    Thank you in advance for your help

  2. #2
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: little shell script

    I hope this is not too late.

    What is happening is that some of the patterns are not being match to a particular file, so they are being passed 'as is' to the mv command. This is also happens on the ls command. For example:
    Code:
    $ ls *.gify
    ls: cannot access *.gify: No such file or directory
    
    $ mv *.gify ./tmp
    mv: cannot stat `*.gify': No such file or directory
    That is the expected behavior of bash as an interactive shell. In scripts the default can be change, so that the not matching expression is expanded as a null string. For that use this in your script:
    Code:
    shopt -s nullglob
    Now in the case of a custom message for each copied file, you would have to copy one file at a time, so you can learn the result of the command mv. This is an example:
    Code:
    #!/bin/bash
    
    # List of patterns.
    FILES='*.jpg *.JPG *.jpeg *.JPEG *.bmp *.BMP *.png *.PNG *.gif *.GIF'
    
    # Destination directory
    DIR=./tmp
    
    # Exapand non maching patterns to null.
    shopt -s nullglob
    
    for file in $FILES; do
    	mv "$file" "$DIR" &> /dev/null
    
    	if [ $? = 0 ]; then
    		echo "$file was found and moved to $DIR."
    	else
    		echo "Unable to move $file to $DIR."
    	fi
    done
    Hope it helps.

  3. #3
    Join Date
    May 2010
    Location
    Alabama
    Beans
    55
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: little shell script

    Dont make it so difficult

    Code:
    #!/bin/bash
    
    PICS='*.jpg *.JPG *.jpeg *.JPEG *.bmp *.BMP *.png *.PNG *.gif *.GIF'
    
    for item in $PICS 
    do 
    find /pics -type f -name $item -exec mv {} /allmypics \;
    done
    Last edited by pafoo; July 9th, 2011 at 03:36 AM.

  4. #4
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: little shell script

    @pafoo: that does NOT work, because $PICS is expanded by globbing in the 'for' line. To debug it, you can replace the 'find' line for 'echo $item' to see what I mean.

    Regards.

  5. #5
    Join Date
    May 2010
    Location
    Alabama
    Beans
    55
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: little shell script

    Not sure what you are talking about. Did you use single quotes? That removes the value of the * bub. When you do the for item in $PICS

    The for loop uses a " " as the delimiter and runs find perfectly, I just ran it on my server as a test and it worked fine.

    My line was

    Code:
    for item in $PICS; do 
    echo $item; find /home/pic_test -type f -name $item
    ;done
    
    *.jpg
    /home/pic_test/pic1.jpg
    
    *.JPG
    /home/pic_test/pic1.JPG
    

    Last edited by pafoo; July 9th, 2011 at 05:27 AM.

  6. #6
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: little shell script

    If there are pics file in the directory in which the script is running, globbing will kick:
    Code:
    $ ls -R
    .:
    dir1/  dir2/  pic1.jpg  pic2.JPG  test9.sh
    
    ./dir1:
    pic3.jpg
    
    ./dir2:
    pic4.JPG
    If you run your script here, $DIR will be expanded and the 'for' won't pass the '*.jpg' and '*.JPG' to the loop, and thus to the find.
    Code:
    $ ./test9.sh 
    mv ./pic1.jpg /allmypics
    mv ./pic2.JPG /allmypics
    Your script won't be able to get to pic3 and pic4.

    I hope it is s bit clear now.
    Regards.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •