/ Sign-up
Your question

Need help with a basic shell script

  • DOS
  • Shell Script
  • Shell
  • Command Prompt
  • Basic
Last response: in Open Source Software
June 20, 2014 6:54:10 PM

Trying to do some basic scripting.

The task:
Create a DOS-like shell on top of the bash shell (so that when you enter "del" it executes the "rm" command in Linux)

I've got all that working fine, my issue, is that when you type in a command that is just a regular Linux command, it should pass it to the bash shell for execution.

This is what I have so far, and again, this works fine for the 'dos-like' commands:
#!/bin/bash #operating in the bash shell

stop = 0 #stop has a value of 0
while [ stop -eq 0 ] #loop when stop is equal to 0
do #start loop
echo - n $HOME\> #prompt is set to home variable
read reply1 reply2 reply3 #read variables for first, second, and third reply.

case $reply1 in
"cd") #when user types "cd", execute change directory command.
rm $reply2 #when user types "del", execute remove command for specified parameter.
mv $reply2 $reply3 #when user types "ren", execute the move (rename) command.
ls | more #when user types "more", execute the display one screen command (ls | more).
cp $reply2 $reply3 #when user types "copy", execute the cp comand for <file> <file>
cp -R $reply2 $reply3 #when user types "xcopy", execute the cp command with recursive parameter.


I'm thinking the best option would be to use an "if" statement, and have anything executable passed to the shell, but I'm not sure how to properly type it and debug it.


if [ $reply1 -x ]
$reply1 | /bin/bash

---------not sure what do to for the else portion?

If anybody could give me any suggestions on a way to have this work, it would be greatly appreciated!

- Carobthomp

More about : basic shell script

June 21, 2014 2:36:25 AM

Why not just use the "alias" command? E.g.:

alias dir=ls
June 21, 2014 6:08:22 AM

Honestly using alias for things like del=rm and Dir=ls is maybe good but things like xcopy=cp not good because command arguments are totally different and being Unix way more powerful
Related resources

Best solution

June 21, 2014 7:59:21 AM

Oh, I agree with that.

Basically I don't see the point in what the OP is trying to achieve. If you are more comfortable with Windows commands than Unix ones then just use Wndows. If you can't be bothered to learn the Unix commands why bother with the OS?

And if you really do want to write your own shell, for whatever reason, use C not shell scripts.
June 21, 2014 4:38:11 PM

It's not that I'm more comfortable with windows commands, it was just an example for an assignment.

If I preferred windows commands, I would definitely use alias.

I was able to figure it out, by adding:

$reply1 $reply2

Thank's for all the advice, and sorry about the confusion.