Page 1 of 4 123 ... LastLast
Results 1 to 10 of 37

Thread: Shell/bash

  1. #1
    Join Date
    Jun 2011
    Beans
    64

    Exclamation Shell/bash

    Hello there,
    I stopped making shell/bash scripts about two years ago due to having more responsibilities in my life. A couple years before that, I made a wonderful script that I wrote on commission for someone who uses torrents - the main purpose of this script was to FIND certain file-types and then do various things with them depending on what the user selected from a menu IN the terminal.
    Problem is that I wrote that script in Ubuntu/Xubuntu 10.04... And for some reason with ABSOLUTELY NO CHANGES it magically stopped working after I upgraded my system, so I'm wondering if anyone can help me make the script work again.

    Keep in mind, it has been multiple YEARS since I used shell/bash which means I'm pretty much a NOOB right now since I forgot most of what I learned thanks to being preoccupied with the military. After you take a look at the script it'll become very obvious what I'm trying to do here - and the part that I'm currently working on is in bold. Everything else is built off of that one part, so it practically makes the entire script worthless.
    Let me know if you have any ideas, as long as you can EXPLAIN how it works to me - if you post an answer and I don't understand what you're trying to tell me, I might send a message asking you about it but I probably won't implement your idea until someone can explain it to me (yes, feel free to treat me like an idiot - just be respectful).

    Again, it's been years since I did anything with code - I still understand some of the basics, but don't throw any advanced or intermediate level coding on me and expect me to understand anything at this time...

    Here's a copy of my NOT-WORKING code:

    Code:
    #!/bin/bash
    # Command to execute is as follows
    #Enter directory with this script, then
    #sh extract.sh /DIRECTORY/WITH/ALL/FOLDERS/CONTAINING/RAR/FILES/
    #DO NOT FORGET THE LAST SLASH
    #For numbers 4-6 you need to edit the path to your own preferred directory...
    echo ""
    echo "Do NOT use numbers 08-10 unless you have modified the script for your"
    echo "personal use!!!"
    echo ""
    echo "to run this script you need to add a directory in which to execute"
    echo "example - sh MYNAME.sh /directory/in/which/I/may/help"
    echo ""
    if [ $# -ne 1 ]
    then
      echo "You forgot to enter directory where I should work!"
      echo ""
      exit
    fi
    
    
    while 
    mesg="\n==============================================\n
      01.. Check .sfv files.\n
      02.. Unrar all rar files.\n
      03.. Delete rar and sfv files.\n
      04.. Extract TAR files.\n
      05.. Extract TAR.GZ files.\n
      06.. Extract TAR.BZ2 files.\n
      07.. Gunzip ZIP files.\n
      08.. Move AVI files to your location.\n
      09.. Move MP4 files to your location.\n
      10.. Move MKV files to your location.\n
      X.. Exit
      \n==============================================\n
    Select: \c"
    do
      echo -e $mesg
      read selection
      case $selection in
      01)
        cd $1
        cfv -r ;;
      02)
        for r in `find $1 -wholename *.rar`
        do
          echo "Unpacking in directory: "`dirname $r`
          unrar e -v $r `dirname $r`
        done ;;
      03)
        for g in `find $1 -wholename *.r*`
        do 
          cd `dirname $g`
          echo "Deleting in directory: "`dirname $g`
          rm *.r?? *.url *.sfv imdb.nfo
          rm -r Sample/
        done ;;
      04)
      # we will need this soon:
        prevdir=`pwd`
        for f in `find $1 -wholename *.tar`
        do
          echo "Unpacking in directory: "`dirname $f`
          # you were using the wrong arguments for tar; any 
          # arguments after the tar file name in your command
          # tar -xvf $f `dirname $f`
          # are treated as files to extract from within the tar.
          # Instead, cd to the directory first, then extract:
          cd `dirname $f`
          tar -zxvf `basename $f`
          # then go back to the dir we were in to start with.
          cd $prevdir
        done ;;
      05)
        # we will need this soon:
        prevdir=`pwd`
        for f in `find $1 -wholename *.tar.gz`
        do
          echo "Unpacking in directory: "`dirname $f`
          # you were using the wrong arguments for tar; any 
          # arguments after the tar file name in your command
          # tar -xvf $f `dirname $f`
          # are treated as files to extract from within the tar.
          # Instead, cd to the directory first, then extract:
          cd `dirname $f`
          tar -zxvf `basename $f`
          # then go back to the dir we were in to start with.
          cd $prevdir
        done ;;
      06)
        # we will need this soon:
        prevdir=`pwd`
        for f in `find $1 -wholename *.tar.bz2`
        do
          echo "Unpacking in directory: "`dirname $f`
          # you were using the wrong arguments for tar; any 
          # arguments after the tar file name in your command
          # tar -xvf $f `dirname $f`
          # are treated as files to extract from within the tar.
          # Instead, cd to the directory first, then extract:
          cd `dirname $f`
          tar -jxvf `basename $f`
          # then go back to the dir we were in to start with.
          cd $prevdir
        done ;;
      07)
        for z in `find $1 -wholename *.zip`
        do
          echo "Unpacking in directory: "`dirname $z`
          gunzip -d -v -r $z `basename $z`
        done ;;
      08)
          for f in `find $1 -wholename *.avi`
        do
          echo "Moving AVI files from: `dirname $f` to /mnt/DATA/Downloads/01-TORRENTS/01-Downloaded/Walker.Texas.Ranger.S08E01-25.DVDRip.XviD-OSiTV"
          find $1/ -name "*.avi"
          mv $f /mnt/DATA/Downloads/01-TORRENTS/01-Downloaded/Walker.Texas.Ranger.S08E01-25.DVDRip.XviD-OSiTV
        done ;;
      09)
          for f in `find $1 -wholename *.mp4`
        do
          echo "Moving MP4 files from: `dirname $f` to /home/masterchief/MOVETESTING"
          find $1/ -name "*.mp4"
          mv $f /home/masterchief/MOVETESTING
        done ;;
      10)
          for f in `find $1 -wholename *.mkv`
        do
          echo "Moving MKV files from: `dirname $f` to /home/masterchief/MOVETESTING"
          find $1/ -name "*.mkv"
          mv $f /home/masterchief/MOVETESTING
        done ;;
      X) 
        exit;;
      esac
    done


    THE PROBLEM is mainly that it STARTS doing what it's supposed to do. Then after it gets done extracting one set of ("r00", "r01", "r02") files and gives me the finished file - such as an ISO or movie file, the script thinks it's done and stops, bringing the menu back up. I don't really understand what the problem is since I haven't changed ANYTHING in the script since I originally created it.
    Loneliness is never more cruel than when it is felt in close propinquity with someone who has ceased to communicate.

  2. #2
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    5,747
    Distro
    Xubuntu 15.04 Vivid Vervet

    Re: SHELL/BASH -- HELP needed!!!

    Moved to programming forum, where it won't get buried so quickly and should find more appropriate readers.
    Sorry I don't think I can help myself.

  3. #3
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,057
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Shell/bash

    Hi rhss6-2011.

    I believe this would be the code you want to fix?
    Code:
        for r in `find $1 -wholename *.rar`
        do
          echo "Unpacking in directory: "`dirname $r`
          unrar e -v $r `dirname $r`
        done
    In general, that would work OK, but only if there's no spaces and special characters on the file names. To avoid those rare cases, I would use this structure:
    Code:
    while IFS= read -d $'\0' -r rarfile
    do
        echo "Unpacking in directory: "`dirname $r`
        unrar e -v $r `dirname $r`
    done< <(find "$1" -type f -iname '*.rar' -print0)
    You could gain some speed and readability, if you only call 'dirname' only once:
    Code:
    while IFS= read -d $'\0' -r rarfile
    do
        unpack_dir="$(dirname "$rarfile")"
    
        echo "Unpacking in directory: $unpack_dir"
        unrar e -v "$rarfile" "$unpack_dir"
    
    done< <(find "$1" -type f -iname '*.rar' -print0)
    Note too that all variables are quoted to avoid problems with spaces and special characters.

    If you are unpacking LOTS of file, it may be also good to use Bash internal parameter substitution instead of a call to 'dirname'":
    Code:
    while IFS= read -d $'\0' -r rarfile
    do
        unpack_dir=""${rarfile%/*}""
    
        echo "Unpacking in directory: $unpack_dir"
        unrar e -v "$rarfile" "$unpack_dir"
    
    done< <(find "$1" -type f -iname '*.rar' -print0)
    Hope it helps. Let us know if that fix the problem or not.
    Regards.

    P.S.1.: I've learned most of these tricks from sisco311 (thanks!)
    P.S.2.: There are other observations I can make, but I imagine your main concern is to get the script working again.

  4. #4
    Join Date
    Jun 2011
    Beans
    64

    Re: Shell/bash

    That wasn't actually my problem - this was (as an example)

    I have a folder called "Torrent Downloads"
    In that folder let's say there are TEN sub-folders.

    In those ten sub-folders are the files I want to extract.


    The script goes into the first sub-folder, extracts the files and then stops because it thinks that the job is done. I currently have no idea how to fix this issue.

    The files are getting extracted properly just the way it should, but for some reason after the first sub-folder is done getting extracted then it stops.

    Any ideas?
    Loneliness is never more cruel than when it is felt in close propinquity with someone who has ceased to communicate.

  5. #5
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,057
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Shell/bash

    Did you try the new code?

    Quick fix: try replacing the find statement in your code
    From:
    Code:
    find $1 -wholename *.rar
    to
    Code:
    find "$1" -type f -iname '*.rar'
    Let us know if that helps.
    Regards.

  6. #6
    Join Date
    Jun 2011
    Beans
    64

    Re: Shell/bash

    I tried that, and I still have the exact same problem; After it extracts the first set of files in the first sub-folder, it stops... This is getting weird - I've never seen this type of problem before...

    Edited to add this:

    I'm not sure if it's a bug in the Shell/Bash coding with Xubuntu/Ubuntu, or if it could possible be a cause from upgrading my system; maybe some of the internal coding changed and my terminal no longer reads the script the way it has for the past year???

    The only changes (aside from your suggestions which I tried implementing) that I've made to this script, are quotation marks. Other than that, literally nothing changed and it stopped working properly, as if by magic after upgrading my system. I'm really confused, to be honest...
    Last edited by rhss6-2011; August 12th, 2014 at 12:46 AM.
    Loneliness is never more cruel than when it is felt in close propinquity with someone who has ceased to communicate.

  7. #7
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,459
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Shell/bash

    maybe give examples of paths that find is supposed to return. Crapshoot is not a valid way of debugging.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  8. #8
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,057
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Shell/bash

    Time to debug.

    Run this:
    Code:
    #!/bin/bash
    
    if [ "$#" -ne 1 ]; then
        echo "Usage ./script.sh path_to_downloads"
        exit 1
    fi
    
    DIR="$1"
    
    echo "----------------------------------------------"
    echo "find $DIR -wholename *.rar"
    echo "Files found: $(find $DIR -wholename *.rar | wc -l)"
    echo
    
    echo "----------------------------------------------"
    echo "find $DIR -iname *.rar"
    echo "Files found: $(find $DIR -iname *.rar | wc -l)"
    echo
    
    echo "----------------------------------------------"
    echo "find "$1" -type f -iname '*.rar'"
    echo "Files found: $(find "$1" -type f -iname '*.rar' | wc -l)"
    echo
    
    echo "----------------------------------------------"
    echo "while IFS..."
    
    rar_count=0
    while IFS= read -d $'\0' -r rarfile
    do
        rar_count=$((rar_count+1))
    
    done< <(find "$DIR" -type f -iname '*.rar' -print0)
    
    echo "Files found: $rar_count"
    echo
    Copy this script on the same directory where your current script is.

    I believe something like this is happening: if there's a rar file on the root directory where the script is executed, Bash would expand the expression '*.rar', and then you won't find all the files (see results marked in red).
    Code:
    $ l -R
    .:
    LAS/  Linux Unplugged/  SciByte/  script.sh*  TechSNAP/
    
    ./LAS:
    LAS.rar
    
    ./Linux Unplugged:
    Linux Unplugged.rar
    
    ./SciByte:
    SciByte.rar
    
    ./TechSNAP:
    TechSNAP.rar
    $ ./script.sh .
    ----------------------------------------------
    find . -wholename *.rar
    Files found: 4
    
    ----------------------------------------------
    find . -iname *.rar
    Files found: 4
    
    ----------------------------------------------
    find . -type f -iname '*.rar'
    Files found: 4
    
    ----------------------------------------------
    while IFS...
    Files found: 4
    
    $ touch dummy.rar
    $ ./script.sh .
    ----------------------------------------------
    find . -wholename *.rar
    Files found: 0
    
    ----------------------------------------------
    find . -iname *.rar
    Files found: 1
    
    ----------------------------------------------
    find . -type f -iname '*.rar'
    Files found: 5
    
    ----------------------------------------------
    while IFS...
    Files found: 5
    
    $ mv dummy.rar LAS.rar
    $ ./script.sh .
    ----------------------------------------------
    find . -wholename *.rar
    Files found: 0
    
    ----------------------------------------------
    find . -iname *.rar
    Files found: 2
    
    ----------------------------------------------
    find . -type f -iname '*.rar'
    Files found: 5
    
    ----------------------------------------------
    while IFS...
    Files found: 5
    Let us know if that helps.
    Regards.
    Last edited by papibe; August 12th, 2014 at 02:35 AM.

  9. #9
    Join Date
    Jun 2011
    Beans
    64

    Question Re: Shell/bash

    Ok, I'm not entirely sure what the heck that did, but... the image should give you an idea of my Downloads folder, as well as whatever the script just figured out, because I sure don't know what it's talking about...

    Something about an unexpected redirect???


    Edited to add this:
    By the way, all the sub-folders have a series of rar files that "need" to be extracted (which is what I'm testing this all on). To give you a visual idea - the image (not thumbnail)...

    (Basically since it's a friend I'm trying to help, I have his folders actually copied on to my hard-drive...)

    Attached Images Attached Images
    Last edited by rhss6-2011; August 12th, 2014 at 03:51 AM.
    Loneliness is never more cruel than when it is felt in close propinquity with someone who has ceased to communicate.

  10. #10
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,459
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Shell/bash

    L2copypaste
    you can highlight-select/middle-click paste
    or if you prefer ctrl+shift+c/v in terminal, ctrl+c/v everywhere else.

    Either way i see in the screenshot that you are running sh explicitly, despite the fact the script is for bash as indicated by the hashbang. sh =/= bash
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Page 1 of 4 123 ... LastLast

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
  •