LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-16-2010, 09:52 PM   #1
Sinensis
LQ Newbie
 
Registered: Jun 2010
Posts: 1

Rep: Reputation: 0
Simple BASH script; understanding loops and case


I'm trying to acquaint myself with shell scripting by following this guide and am getting stuck testing out this script.

All I want it to do is have the script loop continuously, and when I type in a command and 2 optional arguments, it performs the task. Simple way of testing whether I've "got it" or not.

Specific questions:
Am I using the cut command properly?
Is [ ] usable when trying to cut by blank spaces?
Am I using case right?
Is there a way I could have the loop exit if I press ctrl-c or something like that?

Code:
while [ read line ]
do
	command="$(echo $inputline | cut -d[ ] -f1)"
	arg1="$(echo $inputline | cut -d[ ] -f2)"
	arg2="$(echo $inputline | cut -d[ ] -f3)"
	
	case $command in
		copy) & cp &arg1 &arg2
	esac
done
 
Old 06-17-2010, 05:32 AM   #2
Andrew Benton
Senior Member
 
Registered: Aug 2003
Location: Birkenhead/Britain
Distribution: Linux From Scratch
Posts: 2,073

Rep: Reputation: 64
Well your script reads some input into a variable called line but the rest of the script doesn't do anything with the variable $line, it then starts to work with the variable $inputline. line is actually the name of a command so it's a poor choice of variable name.

With cut, I think the correct way of using a space as the field separator is with double quotes
Code:
echo $inputline | cut -d " " -f 2
I can't make out what you're trying to do with the case command, perhaps something like this?
Code:
case $command in
  copy)
    cp $arg1 $arg2
    ;;
esac
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting 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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help understanding a bash script cylon Linux - Newbie 4 09-16-2009 11:04 AM
Case statements and vars for bash script geech Programming 1 01-09-2009 05:49 PM
Bash script: Move files, ignore case Fredde87 Linux - Software 3 09-03-2008 04:34 AM
BASH script with case ( mandriva ) hellreaper Programming 3 02-19-2008 08:36 AM
bash script, understanding variables antis Programming 5 11-15-2007 05:27 AM


All times are GMT -5. The time now is 08:35 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration