Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > macOS > Little Shell Script Help Please....Thanks

Little Shell Script Help Please....Thanks
Thread Tools
BostonMACOSX
Forum Regular
Join Date: Nov 2000
Location: Boston Area,ma
Status: Offline
Reply With Quote
Oct 29, 2002, 05:05 PM
 
Just getting into shell scripting since it can make my life a whole lot easier. although I ran into this problem and I can't see what is wrong is with the script. When I run the script with any input it gives both a 0 and a 1 in return.

Any Ideas?

Thanks
BostonMACOSX

#!/bin/sh

echo "Do you wish to get or put the project:"
read question
echo "your answer was: $question"

if test $question=get
then
echo "0"
fi

if test $question=put
then
echo "1"
fi

exit
OSX ...Plastic surgery for unix
Boston Area Consulting
http://rjhcc.dyndns.biz
http://bostonmacosx.dyndns.org
     
solaris
Fresh-Faced Recruit
Join Date: Apr 2002
Status: Offline
Reply With Quote
Oct 29, 2002, 06:06 PM
 
#!/bin/sh

echo -n "Do you wish to get or put the project: "
read question
echo "your answer was: $question"

case $question in
"get") echo 0
;;
"put") echo 1
;;
*) echo "Invalid option!"
;;
esac
     
Camelot
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status: Offline
Reply With Quote
Oct 29, 2002, 06:09 PM
 
#!/bin/sh

echo "Do you wish to get or put the project:"
read question
echo "your answer was: $question"

if test $question = get
then
echo "0"
fi

if test $question = put
then
echo "1"
fi

exit

Note the spaces before and after the "=" sign in your test statements. Without the spaces, test is seeing if "$question=get" is true. Since it's not null (empty) it will always be true, and therfore the echo statement is run. By adding the spaces, test is able to separate the things you're testing and can correctly compare the strings.

As solaris has already mentioned, though, a case is a neater way of solving the problem.
Gods don't kill people - people with Gods kill people.
     
solaris
Fresh-Faced Recruit
Join Date: Apr 2002
Status: Offline
Reply With Quote
Oct 29, 2002, 06:11 PM
 
By the way, you can use square brackets [] to replace the "test" command:

if [ "$question" = "get" ]; then
...
fi
     
BHD
Junior Member
Join Date: Apr 2002
Status: Offline
Reply With Quote
Oct 30, 2002, 03:27 PM
 
I have another one that I'd appreciate help on, as I am a complete shell-scripting newbie.

Say I want to take a given file, invoke a command on it, and rename it? For example, I want to run an XSLT on foo.xml to create foo.html, or tidy on foo.html to create foo1.html.

How do I do this?

Also, any suggestions on the best tutorial for this kind of thing?
     
BostonMACOSX  (op)
Forum Regular
Join Date: Nov 2000
Location: Boston Area,ma
Status: Offline
Reply With Quote
Oct 31, 2002, 11:17 AM
 
Thanks for the info. Still love this message board.

Last question is if I'm prompted for a command with a UNIX command can I have the shell script answer it with a predefined answer?


Thanks
BostonMACOSX
OSX ...Plastic surgery for unix
Boston Area Consulting
http://rjhcc.dyndns.biz
http://bostonmacosx.dyndns.org
     
solaris
Fresh-Faced Recruit
Join Date: Apr 2002
Status: Offline
Reply With Quote
Oct 31, 2002, 01:32 PM
 
Well if manually you do:

unix prompt% command
command prompt> cmd 1
command prompt> cmd 2
command prompt> exit

You could use a here document, eg:

unix prompt% command <<%
cmd 1
cmd 2
exit
%

This can of course be incorporated into a shell script, just don't indent the limiting string after the commands (in this case %)

Cool huh?
     
Arkham_c
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Oct 31, 2002, 01:58 PM
 
Originally posted by BHD:
I have another one that I'd appreciate help on, as I am a complete shell-scripting newbie.

Say I want to take a given file, invoke a command on it, and rename it? For example, I want to run an XSLT on foo.xml to create foo.html, or tidy on foo.html to create foo1.html.

How do I do this?

Also, any suggestions on the best tutorial for this kind of thing?

#!/bin/sh
# This is myscript.sh

file=$1

# replace touch with the command you want to do to the file
touch $file && mv ${file} ${file}.processed



Then do chmod +x myscript.sh and then run it like:


./myscript.sh fileToProcess
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
BHD
Junior Member
Join Date: Apr 2002
Status: Offline
Reply With Quote
Oct 31, 2002, 03:40 PM
 
Originally posted by Arkham_c:



#!/bin/sh
# This is myscript.sh

file=$1

# replace touch with the command you want to do to the file
touch $file && mv ${file} ${file}.processed



Then do chmod +x myscript.sh and then run it like:


./myscript.sh fileToProcess
Thanks for this!

What I really want to do is to use DropScript to create, well, a drop script. The examples include a gzip script that is the folowing:


gzip -9 "$@"


In my example, I need to use xsltproc not just to invoke the command on the file, but also to specify an output file. So like:


xsltproc -o foo.doc xslfile.xsl foo.xml


Or maybe...


xsltproc -o fooP.xml xslfile.xsl foo.xml


..the "P" in the output file just reflecting that I want something to indicate a processed file.
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 07:28 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,