|
|
Little Shell Script Help Please....Thanks
|
|
|
|
Forum Regular
Join Date: Nov 2000
Location: Boston Area,ma
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Apr 2002
Status:
Offline
|
|
#!/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
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: May 1999
Location: San Jose, CA
Status:
Offline
|
|
#!/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.
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Apr 2002
Status:
Offline
|
|
By the way, you can use square brackets [] to replace the "test" command:
if [ "$question" = "get" ]; then
...
fi
|
|
|
|
|
|
|
|
|
Junior Member
Join Date: Apr 2002
Status:
Offline
|
|
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?
|
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Nov 2000
Location: Boston Area,ma
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Apr 2002
Status:
Offline
|
|
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?
|
|
|
|
|
|
|
|
|
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
Junior Member
Join Date: Apr 2002
Status:
Offline
|
|
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|