0

This is my shell script,which needs to execute any type of command same as terminal,

#!/bin/sh
while
    echo "Reading ..."
    read option
    echo "Its MS : $option"
do
    $option
done

But is not accepting the command which has space in their arguments. like mkdir new\ folder .

So someone help how to arguments with space..

SethMMorton
  • 45,752
  • 12
  • 65
  • 86
Ganesh T
  • 11
  • 1
  • perhaps [this answer](https://stackoverflow.com/a/12811033/4256677) will help – varontron Jan 27 '20 at 04:32
  • Are you reading commands you want to execute line by line? You could use `set -x` to achieve something very similar. – Benjamin W. Jan 27 '20 at 05:12
  • @BenjaminW. Simple example please – Ganesh T Jan 27 '20 at 06:59
  • See [the manual](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set): Put `set -x` before the commands you want to see executed and `set +x` after them. *Are* you reading commands from a file? – Benjamin W. Jan 27 '20 at 15:15
  • @GaneshT : Also, your loop would never terminate (but maybe this is what you want?), becaues you test the exit code of the last `echo` statement, which very likely is `0` always. – user1934428 Feb 24 '20 at 07:56

1 Answers1

0

For me, your script works just fine. The problem you describe comes from something else: Make sure not to use Backslashes for your directories:

Replace mkdir new\ folder with mkdir new/folder, that worked for me.

Make also sure not to have a space within the path to your folder: Not new /folder, rather use new/folder, since mkdir tries to create two folders if you have that space, one in the directory /, another one in your current directory.

Your code works well, but for me it fails if I try to plug in sudo commands.

finnmglas
  • 1,626
  • 4
  • 22
  • 37
  • thanks for your answer. But i want to create a directory with name **new folder** via that script. – Ganesh T Jan 27 '20 at 04:53
  • ` #!/bin/sh option="mkdir new\\ home" $option mkdir new\ home ` Alternately i want same output for both commands... What change i should do in ***option="mkdir new\\ home"*** ? – Ganesh T Jan 27 '20 at 05:06
  • About the folder called "new folder": If you want a space in the folder name, you need to use quotation marks in your command: `mkdir "new folder"`, because the terminal splits arguments by spaces. If you use quotation marks, both "new" and "folder" are kept as one token and interpreted as your new folder name. – finnmglas Jan 27 '20 at 05:47
  • @finnmglas : I think GaneshT is about to open a can of worms here. Maybe he also wants to create a directory `$HOME/tmp`, hoping that it should expand `HOME`? In this case, the command should maybe executed via `eval`, though I then wonder why he needs the whole script and does not simply redefine `PS1` and runs a normal shell. – user1934428 Feb 24 '20 at 07:58