LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Simple BASH script; understanding loops and case (https://www.linuxquestions.org/questions/linux-newbie-8/simple-bash-script%3B-understanding-loops-and-case-814613/)

Sinensis 06-16-2010 08:52 PM

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


Andrew Benton 06-17-2010 04:32 AM

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



All times are GMT -5. The time now is 04:40 PM.