1

I need to create a bash script where a single argument is passed by the user (without using the "read" input option) at the terminal command line that creates a directory with that name, or otherwise notifies the user that such directory exists. Most of the script seems pretty straight forward, except for the input being passed to mkdir WITHOUT read input option.

Things I tried but failed:

1) Prompting the user to input a name for a directory in the terminal and then running the script again and using the history option as input.

2) Attempting to write the users input to a file, only to realize that it requires read.

Any tips or help would be greatly appreciated. Thank you in advance.

closed as off-topic by G-Man, dhag, Archemar, garethTheRed, Stephen Kitt Dec 10 '15 at 9:14

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – dhag, Archemar, garethTheRed, Stephen Kitt
If this question can be reworded to fit the rules in the help center, please edit the question.

2

a single argument is passed by the user (without using the "read" input option) at the terminal command line that creates a directory with that name, or otherwise notifies the user it exists...

It sounds like ordinary positional commandline parameters are what you need:

Script source code for mkdir1:

#!/bin/sh
mkdir "$1"

Usage

./mkdir1 foo
./mkdir1 foo
mkdir: cannot create directory ‘foo’: File exists

foo was created the first time I ran mkdir1. The second time, it notified me that it already exists.

That seems to meet your specification, and the read builtin was not required.

Explanation

$1 is the first positional parameter. $2 would be the second, etc.

For more complex examples than this question requires, you can find more information on positional parameters from many sources, such as the bash hackers wiki, summarized here:

$0           is usually the name of the shell
$FUNCNAME    the function name if inside a function, or $0
$1 - $9      the argument list elements from 1 to 9
${10} - ${N} the argument list elements beyond 9

$*  all positional parameters except $0
$@  all positional parameters except $0
$#  the number of arguments, not counting $0
  • How would I get the user to input their desired name for a directory using positional parameters? – Anton Gorbunov Dec 10 '15 at 0:26
  • I think you should pay attention to ./mkdir1 foo part. In this example foo is delivered to script as $1. – Esref Dec 10 '15 at 0:29
  • I'm sorry but I positively do not get this... :( How do positional parameters allow user input..? – Anton Gorbunov Dec 10 '15 at 0:43
  • @AntonGorbunov after they type the name of the command? – Yet Another User Dec 22 '15 at 2:17

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