PDA

View Full Version : Question about a shell script




mac2x
Sep 24, 2010, 03:08 PM
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.


#! /bin/sh
if pwd == $HOME
then
pwd >> ~/paths.txt
fi


Thanks!



ulbador
Sep 24, 2010, 03:29 PM
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.


#! /bin/sh
if pwd == $HOME
then
pwd >> ~/paths.txt
fi


Thanks!


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:


#! /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

mac2x
Sep 24, 2010, 03:41 PM
Thank you for the help! As I said I'm a n00b at this. I'll try your suggestions and read the webpage. :)

mac2x
Sep 24, 2010, 03:49 PM
Oh, BTW, $HOME is set on my machine. However, your script returns this error:

line 8: [[/Users/myname: No such file or directory

chown33
Sep 24, 2010, 04:29 PM
Oh, BTW, $HOME is set on my machine. However, your script returns this error:

line 8: [[/Users/myname: No such file or directory

There should be a space between [[ and what follows, and another space before ]]. Example:
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)).

mac2x
Sep 24, 2010, 04:34 PM
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?

seepel
Sep 25, 2010, 04:04 AM
There's also a handy environment variable for this, I would just do this...

if [ "$PWD" == "$HOME" ]; then
pwd >> "~/paths.txt"
fi


Note that the double quotes will protect you against directories that might contain a space.