Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 24, 2010, 03:08 PM   #1
mac2x
macrumors 65816
 
Join Date: Sep 2009
Question about a shell script

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. 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!
__________________
Retina MBP | 2.6 GHz i7 | 16 GB RAM

Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE
mac2x is offline   0 Reply With Quote
Old Sep 24, 2010, 03:29 PM   #2
ulbador
macrumors 68000
 
ulbador's Avatar
 
Join Date: Feb 2010
Quote:
Originally Posted by mac2x View Post
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. 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!

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

Last edited by ulbador; Sep 24, 2010 at 03:38 PM.
ulbador is offline   0 Reply With Quote
Old Sep 24, 2010, 03:41 PM   #3
mac2x
Thread Starter
macrumors 65816
 
Join Date: Sep 2009
Thank you for the help! As I said I'm a n00b at this. I'll try your suggestions and read the webpage.
__________________
Retina MBP | 2.6 GHz i7 | 16 GB RAM

Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE
mac2x is offline   0 Reply With Quote
Old Sep 24, 2010, 03:49 PM   #4
mac2x
Thread Starter
macrumors 65816
 
Join Date: Sep 2009
Oh, BTW, $HOME is set on my machine. However, your script returns this error:

Code:
line 8: [[/Users/myname: No such file or directory
__________________
Retina MBP | 2.6 GHz i7 | 16 GB RAM

Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE

Last edited by mac2x; Sep 24, 2010 at 04:17 PM.
mac2x is offline   0 Reply With Quote
Old Sep 24, 2010, 04:29 PM   #5
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by mac2x View Post
Oh, BTW, $HOME is set on my machine. However, your script returns this error:

Code:
line 8: [[/Users/myname: No such file or directory
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/libra...n1/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)).
chown33 is offline   0 Reply With Quote
Old Sep 24, 2010, 04:34 PM   #6
mac2x
Thread Starter
macrumors 65816
 
Join Date: Sep 2009
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?
__________________
Retina MBP | 2.6 GHz i7 | 16 GB RAM

Gaming Rig | Core i7-2600K | 16 GB RAM | 160 GB OCZ Agility 2 | EVGA GTX 770 SC | Windows 7 Pro x64 | ASUS VG248QE
mac2x is offline   0 Reply With Quote
Old Sep 25, 2010, 04:04 AM   #7
seepel
macrumors 6502
 
Join Date: Dec 2009
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.
__________________
Free iOS App that turns your music library into an info graphic.
Phonographic
seepel is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Apple script batch shell script prramesh Mac Programming 5 Sep 15, 2013 03:35 AM
AudioScope.sh shell script... wisecracker MacBook Pro 0 Jul 31, 2013 01:31 PM
Shell Script to Sync iPhoto ajbudis Mac Programming 0 Jun 30, 2013 12:52 PM
Anyone know how to use AppleScript or shell-script? gba2000 Mac Applications and Mac App Store 0 Apr 23, 2013 11:14 AM
Need help with a shell script thatgreenvw Mac Programming 1 Nov 30, 2012 07:55 AM

Forum Jump

All times are GMT -5. The time now is 09:52 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC