Question about a shell script

Discussion in 'Mac Programming' started by mac2x, Sep 24, 2010.

  1. macrumors 65816

    Joined:
    Sep 19, 2009
    #1
    Hi all!

    I'm writing a shell script that will run pwd on the directory I am in and write the output to a text document for later use. However, I'm running into a couple of things I don't want it to do; I'm attributing that to lack of experience. :>)

    First, the output of pwd is being printed to the screen as well as being written to the file. I know the problem is with the pwd == $HOME statement because if I remove it and the if statement leaving pwd >> paths.txt, the output is only printed to the file ~/paths.txt.

    Second, I would like to make this script work only in the user's home directory. Mainly as an exercise in using if statements. :D Is there any way to do this? As it is now, it works in all directories. Nothing wrong with that, of course, but I don't need an if statement to make it work that way.

    Code:
    #! /bin/sh
    if pwd  == $HOME
    then
            pwd >> ~/paths.txt
    fi
    
    Thanks!
     
  2. macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #2

    Well, you have a few problems. First off, you are executing the command, not executing and storing the results.

    The results you are seeing printed out are coming from the execution in the "if" statement.

    The reason it "works" is because it is failing on the "if" every time. Also, and this is probably nitpicking, but I'm used to bash, which is probably what you are in, so you could use some formatting help here:

    Code:
    #! /bin/bash
    
    #this line executes the pwd command and puts it in a variable named path.  Those are backticks, next to the "1" button.
    #If you just put pwd, it will actually execute it as though you typed it and display the results to STDOUT
    path=`pwd`
    
    #this SHOULD work.  I'm not near a bash interpreter right now so Im pulling from memory
    if [["$path" = "$HOME"]]; then
    echo $path >> ~/paths.txt
    fi
    
    I'm also not sure if this will work either, is $HOME set? If you want to be sure just drop in echo $path and echo $HOME before the if to see what they are.

    Here is a decent link I found about comparison in bash

    http://tldp.org/LDP/abs/html/comparison-ops.html
     
  3. thread starter macrumors 65816

    Joined:
    Sep 19, 2009
    #3
    Thank you for the help! As I said I'm a n00b at this. I'll try your suggestions and read the webpage. :)
     
  4. thread starter macrumors 65816

    Joined:
    Sep 19, 2009
    #4
    Oh, BTW, $HOME is set on my machine. However, your script returns this error:

    Code:
    line 8: [[/Users/myname: No such file or directory
     
  5. macrumors 603

    Joined:
    Aug 9, 2009
    #5
    There should be a space between [[ and what follows, and another space before ]]. Example:
    Code:
    if [[ "$path" = "$HOME" ]]; then
    

    If you're not referring to the bash man page, or some other bash reference guide, you should be. Shell programming is quirky. Trying to learn it without at least the shell language reference guide is simply futile.

    For an example, visit the online man page:
    http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/bash.1.html

    Then find the string [[ on the page, where it explains what [[ ]] does. Notice there is a space between [[ and its expression. For comparison, directly above it, notice there isn't a space for ((expression)).
     
  6. thread starter macrumors 65816

    Joined:
    Sep 19, 2009
    #6
    Thanks!

    I have the large O'Reilly Classic Shell Scripting book, and it is a good book, but seems to focus on bourne shell primarily. I'll look at the man pages. Is there a better book for bash?
     
  7. macrumors 6502

    seepel

    Joined:
    Dec 22, 2009
    #7
    There's also a handy environment variable for this, I would just do this...
    Code:
    if [ "$PWD" == "$HOME" ]; then
        pwd >> "~/paths.txt"
    fi
    
    Note that the double quotes will protect you against directories that might contain a space.
     

Share This Page