Results 1 to 9 of 9

Thread: shell script help?

  1. #1
    Join Date
    Apr 2007
    Beans
    Hidden!
    Distro
    Ubuntu 7.04 Feisty Fawn

    shell script help?

    hey guys,

    can someone help me do the following?

    I need to read in the first and only line in myfile.txt which will be something like "case1" or "case2", etc. Then I need to create a directory with the name of that string which was read in (ie. a folder called case1).

    I can read the line from the file using:
    grep "case" myfile.txt

    but I can't figure out how to use that and make the folder.

    Thanks!

  2. #2
    Join Date
    Apr 2006
    Beans
    275

    Re: shell script help?

    Sounds like a homework assignment!

  3. #3
    Join Date
    May 2005
    Beans
    Hidden!
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: shell script help?

    How about

    name=`grep case file.txt`
    mkdir $name

    regards
    Jergar

  4. #4
    Join Date
    Apr 2007
    Beans
    Hidden!
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: shell script help?

    Quote Originally Posted by Stephen Howard View Post
    Sounds like a homework assignment!
    it's amazing how many times you can ask a question on this topic and get this answer ... it gets really old.

    Anyway it is for part of one of my thesis codes. I need to randomize the folder which contains the input files because I will be using a quadcore processor and it may end up uploading muliple sets of input files (different cases) at the same time which will override the other.
    Last edited by elfstone214; June 6th, 2007 at 05:22 PM.

  5. #5
    Join Date
    Apr 2007
    Beans
    Hidden!
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: shell script help?

    Quote Originally Posted by Jergar View Post
    How about

    name=`grep case file.txt`
    mkdir $name

    regards
    Jergar
    Jergar,

    thanks but I tried that you said and it created a directory called grep, and another called case

  6. #6
    Join Date
    Jun 2007
    Location
    Tacoma, WA
    Beans
    244
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: shell script help?

    Quote Originally Posted by elfstone214 View Post
    Jergar,

    thanks but I tried that you said and it created a directory called grep, and another called case
    It sounds like you used quotes instead of backticks.

    Quote: '
    backtick: `

    The difference is subtle, which leads to some frustrating misunderstandings when reading or copying a script. The backticks are instructions to the shell to run a command in a sub-shell. The output of the sub-shell command is returned and can be assigned to a variable. In this case, the variable name was assigned the output of : grep case file.txt

    This type of backtick substitution is useful, and used fairly often in shell scripting. Be aware that grep returns nothing if there is no match, and multiple values if there are multiple matches. Because of that, consider:

    Code:
    name=`head -n 1 file.txt`
    if [ -z $name ]; then
       mkdir $name
    fi
    This block of code will read the first line of the file, and then check whether or not name recieved an assignment.

  7. #7
    Join Date
    Mar 2007
    Location
    Your Closet
    Beans
    380
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: shell script help?

    Quote Originally Posted by meatpan View Post
    It sounds like you used quotes instead of backticks.
    name=`head -n 1 file.txt`
    if [ -z $name ]; then
    mkdir $name
    fi
    "-z" is zero length, "-n" is nonzero. I think you wanted "-n" here. Also, you forget to quote your variable in that test. And here's my solution to the backtick confusion:

    ...

  8. #8
    Join Date
    Apr 2007
    Beans
    Hidden!
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: shell script help?

    thanks guys!

    you were right I was using quote instead of backstick ... I have fallen for this more than once already

  9. #9
    Join Date
    Jun 2007
    Location
    Aarhus, Denmark
    Beans
    11
    Distro
    Kubuntu 9.04 Jaunty Jackalope

    Re: shell script help?

    you were right I was using quote instead of backstick ... I have fallen for this more than once already
    Yes backticks are a pain. Sometimes they are on dead keys. Fortunately, bash provides a special variable to do the same:

    name=$(grep case text.txt)

    I find that more readable than backticks.

    Cheers,
    Morten

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
  •