1

I need to make Bash take DOS commands in with arguments and then convert the main command to a Bash command and still use the arguments if there are any arguments.

The part I am lost on is how to pass arguments to the individual cases and not getting stuck by the *) Command Not Found!

For example a user inputs:

copy file1.txt file2.txt

Use case: copy then should run the Linux cp command and use the two arguments passed copy to finish the command.

#!/bin/bash

while :
do
read INPUT_STRING
case $INPUT_STRING in
        chdir|CHDIR)
        cd $arg1
        bash myscript.sh
;;
        cls|CLS)
        clear
        bash myscript.sh
;;
        copy|COPY)
        cp $arg1 $arg2
        bash myscript.sh
;;
        createdir|CREATEDIR)
        mkdir $arg1
        bash myscript.sh
;;
        delete|DELETE)
        rm $arg1
        bash myscript.sh
;;
        dir|DIR)
        ls
        bash myscript.sh
;;
        move|MOVE)
        mv $arg1 $arg2
        bash myscript.sh
;;
        print|PRINT)
        echo $arg1
        bash myscript.sh
;;
        quit|QUIT)
        break
        PS1="n01396736@cisvm-cop4640-2:~$ "
;;
        rename|RENAME)
        mv $arg1 $arg2
        bash myscript.sh
;;
        type|TYPE)
        cat $arg1
        bash myscript.sh
;;
        *)
        echo "Command Not Found!!"
        bash myscript.sh
;;
esac
break
done
  • 2
    What are you expecting bash myscript.sh at the end of each case branch to do? I think you're trying to loop, but you're actually calling the script from itself over and over again. Remove the break at the end and strip out all these bash myscript.sh statements. – roaima Feb 14 at 23:59
  • 1
    Quote those variables. Quote those variables. Quote those variables. (For example mv $arg1 $arg2 should be rewritten as mv "$arg1" "$arg2") – roaima Feb 15 at 0:00
  • I need it to continue to loop until user types quit or QUIT. with a single break in the quit|QUIT) case it didn't send the user back the terminal/command prompt out of the bash. As far as the bash myscript.sh I had issues where it would run dir and then drop the user to back to the terminal/command prompt out of the bash. – Trinity Zamrzla Feb 15 at 0:12
  • I added the quotes around all the $arg1 and $arg2. I am still getting the same error that when I input copy file1.txt file2.txt my catch all *) responds with Command Not Found!! which is a good thing that it is working, however, I need the arguments to be sued inside the case. In java or C I could separate the input by white space and then store them into an array and then increment through the array to read each section separately. – Trinity Zamrzla Feb 15 at 0:35
  • Thank you for the hit about the quotes I got it working now. read command arg1 arg2 arg3 case "$command" in then I am using "$arg1" to "$arg3" as needed – Trinity Zamrzla Feb 15 at 1:37
2

Thank you roamia and Michael Prokopec for pointing me in the right direction. This is what I ended up with and it works like it is suppose to. If you see any errors or things I can do to improve the code please let me know. This is my first Linux script I have created.

#!/bin/bash

counter=1
while : [$counter -lt 2]
do
read -p 'Your Name Here > ' cmd "arg"

case $cmd in
chdir|CHDIR)
        cd $arg
        ;;
cls|CLS)
        clear
        ;;
copy|COPY)
        cp $arg
        ;;
createdir|CREATEDIR)
        mkdir $arg
        ;;
createfile|CREATEFILE)
        touch $arg
        ;;
delete|DELETE)
        rm $arg
        ;;
dir|DIR)
        ls
        ;;
move|MOVE)
        mv $arg
        ;;
print|PRINT)
        echo $arg
        ;;
quit|QUIT)
        counter=2
        ;;
rename|RENAME)
        mv $arg
        ;;
type|TYPE)
        cat $arg
        ;;
*)
        echo "Command Not Found!!"
        ;;
esac
done
  • Looks good, glad you got it working. Have a great day. – Michael Prokopec Feb 19 at 14:50
2

Your script seems to simply replace command names with alternative names. This is easily done by providing aliases in the shell.

For example:

alias remove=rm
alias type=cat
alias dir='ls -l'
alias cls=clear
alias quit=exit

You could put these into a file called something like dos_compat and then start an interactive bash shell using

bash --rcfile dos_compat

This would start a new interactive shell with the aliases available. Use exit or quit or Ctrl+D to get back to your original shell.

If any DOS command that you'd like to implement is more complex than a simple alias, you would need to implement that as a shell function in the dos_compat file. One use for this would possibly be to automatically detect DOS-like pathnames in filename arguments and change things like C:\ into /, or rearrange arguments for more complex DOS commands.

The benefit of doing it this way would be that you would still be able to use Unix shell commands alongside the DOS aliases, and you would also be able to use DOS commands in more complex compound shell commands constructs, if you would wish to.

1

Why not write a script that adds aliases to your .bashrc for those DOS commands and map the commands to the appropriate Bash counterpart. You could also make it so you could easily remove those entries with a script, so you could basically migrate your bash environment from computer to computer.

You could also make it so that it moves the original .bashrc to .bashrcbak and puts yours in place then when you want to revert just have it move the original back.

Or you could make single session aliases like found here: https://unix.stackexchange.com/a/148928/321418

  • unfortunately I don't know if the professor would want me to do that. This is for a class assignment and unfortunately he gives no instructions to complete the assignments just search the internet. – Trinity Zamrzla Feb 15 at 1:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.