Congratulations to AcaCandy on her 100,000th post!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
acer black screen blue screen boot bsod computer connection crash css dell display drive driver drivers email error ethernet excel firefox firefox 3 game hard drive internet internet explorer itunes laptop linux malware monitor network networking nvidia outlook outlook 2003 outlook 2007 outlook express partition problem router slow software sound trojan usb video virus vista windows windows xp wireless
UNIX/Linux
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Operating Systems > UNIX/Linux >
Bash Scripting


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
oldshep's Avatar
Junior Member with 22 posts.
 
Join Date: Jul 2004
04-Jul-2004, 07:19 AM #1
Angry Bash Scripting
I'm new to scripting:

The script below works fine:

#!/bin/sh
echo "Hello World"
if [ "$1" ]
then
echo "arg passed was $1"
else
echo "no arg passed"
fi

The following script does not work:

#!/bin/sh
if [ "$1" ]
then
echo " argument recieved value is: $1"
#case $1 in
# [a-z]*) echo "name starts with a letter";;
# [0-9]*) echo "name starts with a number";;
# *) echo "name starts with invalid character";;
#esac
else
echo " This script requires a student name"
fi

If from prompt: scriptname

get the message::bash interpreter: no such file or directory

If from prompt: ./scriptname

get the message::bash interpreter: no such file or directory

If from prompt: sh scriptname

get the message::sciriptname:line2 [:missing ']'


Anyone What's the probelem?
lynch's Avatar
Senior Member with 1,962 posts.
 
Join Date: Aug 2002
Location: Back East,Way Back East
04-Jul-2004, 08:26 AM #2
For one thing, it looks like you need to uncomment(#) lines in the case construct:
Code:
 #case $1 in
# [a-z]*) echo "name starts with a letter";;
# [0-9]*) echo "name starts with a number";;
# *) echo "name starts with invalid character";;
#esac
lynch
oldshep's Avatar
Junior Member with 22 posts.
 
Join Date: Jul 2004
04-Jul-2004, 10:41 PM #3
bash scripting
the reason the case constuct was commented out was to isolate that bit of coding. The shell then looks the same as the one that workrd OK but this one stil ldid not work...that's my problem
Squashman's Avatar
Distinguished Member with 12,689 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
05-Jul-2004, 12:03 AM #4
You need to test whether or not there is variable be passed from the shell prompt.

I think this is what you need to do.
Code:
#!/bin/sh
if [ -z "$1" ]
then
       echo " This script requires a student name"
       exit 1
else
    echo " argument recieved value is: $1" 
case $1 in
[a-z]*) echo "name starts with a letter";;
[0-9]*) echo "name starts with a number";;
*) echo "name starts with invalid character";;
esac
fi
I am not sure if this is quite correct. CodeJockey will be along sometime this week to clean up my code. I have not tested this. I dont think that case statement is going to work either.
__________________
I hate asking the same question twice!
How to ask questions the smart way!
Microsoft MVP - Windows Shell/User

Last edited by LwdSquashman : 05-Jul-2004 12:11 AM.
codejockey's Avatar
Senior Member with 1,410 posts.
 
Join Date: Feb 2002
06-Jul-2004, 03:47 PM #5
I suspect that spacing is your problem (as incredible as that sounds!). When you use the '[' and ']' symbols, you are actually using a shorthand form of the 'test' command, and you must use a blank character both before and after either '[' or ']' symbol. So, for example, the following is fine:
Code:
if [ "$1" ]
then
    echo $1 has the value $1
fi
but the following is not fine:
Code:
if [ "$1"]     # note the missing space after the second quote
then
    echo $1 has the value $1
fi
Hope this helps.
__________________
The slowest component still sits at the keyboard.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 03:30 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.