Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Shell Scripting - Help!

  • 17-07-2001 12:47pm
    #1
    Registered Users, Registered Users 2 Posts: 4,676 ✭✭✭


    Hum I could be wrong, but the end of your script looks a bit suss.


    perhaps

    for TEMP in `ls` // or *
    do
    [ "$TEMP" == "$filesearch" ] && mv $TEMP ./$filesearch
    done

    That will work ( hum maybe ), and it is in line with your current script.. but it fairly cumbersome as you can see. If you want to account for wildcards, maybe grep would be better than the == operator

    Gav


Comments

  • Registered Users, Registered Users 2 Posts: 432 ✭✭Catch_22


    line 6: missing $ on filesearch

    for "${filesearch}*" in `ls`

    this is incorrect the * should not be there

    mv "${filesearch}*" ./$filesearch

    same again remove the *

    i generally dont use cases so im not sure of the syntax there

    c22


  • Closed Accounts Posts: 28 teac!


    I think you're going about the problem kinda wrong.
    First of all I think your methodology needs a bit of a rethink along with some of the way you're designing the program.

    1. You want to read in a string which will be used to match a filename.
    Therefore if you read in the string "ab" or "blah", this could match more than one file in the directory, what do you do in this case? Or is this even a possbility?
    If it's not a possibility, then we can continue generally along the same lines as what you want.

    What I presume you have is a directory with a list of files such as

    fart1.jpg fart2.jpg fart3.jpg jump1.jpg jump2.jpg jump3.jpg

    You want the "fart" JPG files to go into a directory called fart/ and the "jump" JPG files to go into a directory called jump/

    One of the first things is that when using shell scripts, use

    #!/bin/sh -x

    This -x switch puts you into a sort of debugging mode, which helps with debugging scripts.

    if [ -d $filesearch ]

    When referencing variable names, use $. I presume this was a typo or simple mistake since you use it below this, but just in case.

    Personally what I'd do is

    for file in `ls`
    do
    if [ $(echo $file | grep $filesearch) ]; then
    mv $file $filesearch
    fi
    done


    I don't have a machine to test this on, I'll have a gander at it tomorrow maybe, but that's the kind of approach you want. Your wildcard solution is a bit awkward to implement compared to other solutions, and you're going about it the wrong way as well.

    Echo'ing the filename and using grep to match the filesearch variable in the filename, will work. It will also move every instance of matching filenames into the directories.

    If you're still having trouble with this, I'll write something up properly, unfortunately I can't test this on the machine I'm using at the moment. Also when posting help about something like this, it often helps to post a sample problem (i.e. the directory of files, or a sample of the files, in the directory and what you want to do with them. Just for future reference like smile.gif )

    Regards,
    Phil.


  • Closed Accounts Posts: 752 ✭✭✭Loomer


    Excellent Stuff - Thanks All! biggrin.gif I am just reading a book on shell scripting doing the examples and then trying to make up problems and solve them to see where the pitfalls are. I find this works best for me retaining the stuff I've done. The stuff you guys have put I understand and makes sense as being more effecient. Can anyone suggest any good books for shell scripting - I hear Mastering Regular Expressions by O'Rielly is good.


  • Closed Accounts Posts: 752 ✭✭✭Loomer


    I just learning the ropes of shell scripting and am trying to do something. I might be barking up the wrong tree so please tell me. I am trying to write a script that reads in a string called variable string (for example)
    I then want to search the current directory for any file containing that string and move them into a directory which the script has already created using the variable value.
    If I havent lost you yet I want to know is it possible to append wildcards , * to the front and back of the variable? e.g $*string*. I tried searching on this and thought I had the answer as $*{string}* bu tnot working.
    Am I going about this all the wrong way. Is it even possible with a shell script and is there a better way to do it? Any help is much appreciated. biggrin.gif

    This is what I have so far.

    #!/bin/sh

    echo "Please enter the file name you wish to search for"
    read filesearch

    if [ -d filesearch ]
    then
    echo "There is a directory with this name."
    else
    echo "There is no directory with this name."
    echo "Would you like to create one, y/n"
    fi

    read y_or_n

    case "$y_or_n" in
    y ) mkdir "$filesearch";;
    n ) echo "No folder was created";;

    * ) echo "Please answer y or n"
    exit 1;;

    esac

    for "${filesearch}*" in `ls`
    do
    mv "${filesearch}*" ./$filesearch
    done

    exit 0


Advertisement