Visit Our UNIX and Linux User Community


newb shell question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting newb shell question
# 1  
Old 12-13-2001
CPU & Memory newb shell question

I know this is a total begginer question but what is wrong with this picture. My loop wont loop, it just drops me back to the prompt and gives the error "unary operator expected". Any help would be appreciated.

here it is,

#!/bin/sh
selection=
while [ $selection -ne9 ]
do
cat << MENU
1)List files in current directory
2)Display pwd
3)Remove a directory
4)Create a directory
5)Copy a file
6)Remove a file
7)Change directory
8)Mail file to user
9)EXIT
MENU
echo "Enter selection:\c"
read selection
echo "the user selected option $selection"
if [ $selection -eq 1 ]
then
ls
elif [ $selection -eq 2 ]
then
pwd
elif [ $selection -eq 3 ]
then
echo "Name of dir to delete"
read option3
rm -r $option3
elif [ $selection -eq 4 ]
then
echo "Name of dir to create"
read option4
mkdir $option4
elif [ $selection -eq 5 ]
then
echo "Name of file to copy"
read option5
echo "Where to move it to?"
read option55
cp -r $option5 $option55
elif [ $selection -eq 6 ]
then
echo "Name of file to delete"
read option6
rm -r $option6
elif [ $selection -eq 7 ]
then
echo "Name of directory to change to"
read option7
cd $option7
pwd
elif [ $selection -eq 8 ]
then
echo "Name of file to send"
read option8
echo "Who to send it to"
read option88
mailx $option8 < $option88
elif [ $selection -eq 9 ]
then
exit
else
echo "Please enter a valid selection"
fi
done
# 2  
Old 12-13-2001
The first think I'm seeing is the while statement at the top...

selection=
while [ $selection -ne9 ]

First one:
I always use quotes - to be safe.
And check your spacing...
while [ "$selection" -ne "9" ]

Also though, you just defined $selection as being nothing... it would look like this when expanded:

while [ -ne 9 ]

Try:
selection=0
while [ "$selection" -ne "9" ]

Also, it'll make it cleaner to use a "case" statement, instead of a bunch of "if"s...

case $selection in
1) do_something ;;
2) do_something_else ;;
*) default_at_end ;;
esac

Hope that gets you back on the right track...
# 3  
Old 12-13-2001
Bug

Thanks for the pointers, Ive tweaked it and it seems to be working now. But at 3am last night i had no idea what was going on. lol

Previous Thread | Next Thread
Test Your Knowledge in Computers #164
Difficulty: Easy
Developers began to abandon MINIX after free and open-source Unix-like operating systems such as Linux and 386BSD became available in the early 1990s.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

NEWB Question - BASH COMMAND RESULT for ${0##*/}

This should be extremely simple and someone will probably answer it in .5 seconds. I need to know what: VARIABLE=${0##*/} does? I do not have a shell handy to just try it in. I am reading through some scripts and need to understand this line. Any help is appreciated. Many thanks! -... (3 Replies)
Discussion started by: chrisgoetz
3 Replies

2. Shell Programming and Scripting

Newb question about getting a word from a text file

Hi everyone. I am new to shell scripting and have been looking at quite a few web pages to try and figure this out, but to no avail. What I am trying to do is get a value from a text file that contains a paragraph of information.. Something similar too: Welcome to random script You are... (1 Reply)
Discussion started by: elemenopee
1 Replies

3. UNIX for Dummies Questions & Answers

Newb writing his first shell script

Hey! This is my first post on this forum, nice to meet ya! I've been using Linux for a good few years, I grew up using DOS and a few similar CLI-based OS's so I'm fairly okay with navigating my way around the terminal. Recently I decided I wanted to become a sysadmin so I've been teaching... (3 Replies)
Discussion started by: Tamachan87
3 Replies

4. HP-UX

Newb Help, need to image one HD to another

Hello, I am totally new to Unix We have a piece of test equipment, three of them actually, that run HP-UX 9.0 from like 1994. We had backup tapes with procedures to load the OS and our test software, but the 16-track tapes are corrupt now. HP will not support it, they dont have it anymore,... (11 Replies)
Discussion started by: Newball80
11 Replies

5. Shell Programming and Scripting

Newb with While loop question

My first post here.... I have a few years exp with linux distros and some very basic Python..Ive been intent on learning shell scripting the last few weeks. Please excuse my crude efforts. I am running a program that takes network data containing US city names in plain text. I am TRYING to... (7 Replies)
Discussion started by: dddkkk
7 Replies

6. Shell Programming and Scripting

Another newb question: how to use test for zero-length string ?

Assume $x equals "". If I try: if test -n $x I get the "Expression syntax" error. It works in Linux but not in unix bash. In unix bourne I get "test: argument expected" (4 Replies)
Discussion started by: lumix
4 Replies

7. Shell Programming and Scripting

Newb scripting question, I get the error script not found

This is probably a really simple problem, but goes easy on me I'm still a newb. The problem I have is that a script (we'll call it script.script) I edited won't run for some reason, I get the error "ksh: script.script: not found" The location of my script is as follows: /home/users/arkitech ... (3 Replies)
Discussion started by: Arkitech
3 Replies

8. UNIX for Dummies Questions & Answers

Ok I'm a Newb, Please Help

I am wanting to download the Linux Program. When i go to download it I see several things to download. What do i download exactly? Then what do I do to install it. Also I have partitioned my hard drive to make way for Linux on the other part to play with it, will any problems arise from this? I... (3 Replies)
Discussion started by: Seeto
3 Replies

9. UNIX for Dummies Questions & Answers

Newb question

I don't even know if this should go here but I just would like to know what this means: d0e45878043844ffc41aac437e86b602 I know absolutely nothin' about UNIX, and I found this in a SQL table in a board I run. Someone please tell me what that is in "normal" mode. Pardon me for my... (4 Replies)
Discussion started by: daeglin
4 Replies

10. UNIX for Dummies Questions & Answers

help for a newb

ok i just installed FreeBSD 4.8 on a computer i had lying around and it was working ok but then when i tried to set up KDE's xdm (kdm) i think i must have configured the login manager wrong because i cant login through the kdm and therefore i can log in to BSD at all?! please can someone help me... (5 Replies)
Discussion started by: h3x
5 Replies

Featured Tech Videos