Login or Register to Ask a Question and Join Our Community

Creating a new directory by getting input from user

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Creating a new directory by getting input from user
# 1  
Old 02-08-2014
Creating a new directory by getting input from user

Hi All,

I am really new to Linux.I am trying to write a script for creating a new directory by getting input of folder name from the user.Please help me in this regard.

#! /bin/bash
echo "Enter name of dir":$filename
mkdir -p $filename

When executing this I am getting following error

pradeep@pradeep-VirtualBox:~/sample$ sh test.sh 123
Enter name of dir:
mkdir: missing operand

I am using Ubuntu running in virtual box.


Last edited by bartus11; 02-08-2014 at 07:51 AM.. Reason: Please use [code][/code] tags.
# 2  
Old 02-08-2014
Welcome to the forums...

Try :

#! /bin/bash
echo "Enter name of dir":
read filename
mkdir -p $filename

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 02-08-2014
Thanks a lot..Smilie
Can u also please suggest some good sites,books for beginners in Shell Scripting..
Thanks again.
# 4  
Old 02-08-2014
As I always suggest unix.com is the best place for learning, this forum has got prompt support,and you can post your doubts here, our forum members will help, and you can go through this book also Classic Shell Scripting - O'Reilly Media
# 5  
Old 02-08-2014
As an addendum to Ashkay's post...
ALWAYS remember to check if the directory/folder/drawer exists or not to prevent
unwanted error reports.
Checking applies to files, etc, across the board to prevent major script exits too.
Here is a very simplified version of such s task longhand from an OSX 10.7.5 bash
Last login: Sat Feb  8 12:50:06 on ttys000
AMIGA:barrywalker~> drawer="/Users/barrywalker/Desktop/Code"
AMIGA:barrywalker~> if [ -d "${drawer}" ]
> then
> echo "$drawer exists..."
> else
> echo "Create your drawer in this section..."
> fi
/Users/barrywalker/Desktop/Code exists...
AMIGA:barrywalker~> drawer="/Users/barrywalker/Code"
AMIGA:barrywalker~> if [ -d "${drawer}" ]
> then
> echo "$drawer exists..."
> else
> echo "Create your drawer in this section..."
> fi
Create your drawer in this section...
AMIGA:barrywalker~> _

Hope this helps...
This User Gave Thanks to wisecracker For This Post:
# 6  
Old 02-08-2014
To expand on what wisecracker said: whenever you write a script - especially one which depends on user input - you should strive to have an answer to every problem that might happen. The script does not have to solve the problem, but it has to notice it and report the error condition because of which it couldn't do what it was supposed to do.

What can possibly go wrong in creating a directory:

- directory already exists
- supposed name is malformed (like in containing "/", for instance)
- parent directory does not exist (you dealt with that by using "-p", but is that a wise decision?)
- script is not allowed to create directory
- filesystem is full (ran out of inodes)

and probably some obscure reasons more.

I hope this helps.

These 2 Users Gave Thanks to bakunin For This Post:
# 7  
Old 02-08-2014
Thanks a lot Guys..

@wisecrackerSmiliene question though..

if [ -d "${drawer}" ]

What does this line do ?

Thanks all again..Smilie

Last edited by radoulov; 02-11-2014 at 05:04 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Prevent user from creating new user from his login

Hi Experts, Need your support Redhat 6.5 I want to create a user with all(read, write, execute) privileges except that user should not be able to create any new user from his login to perform any task. (10 Replies)
Discussion started by: as7951
10 Replies

2. Shell Programming and Scripting

User input and run awk using the input

I am trying to allow a user to enter in text and then store that text in a variable $gene to run in an awk command in which those values are used to run some calculations. I am getting syntax errors however, when I try. Thank you :). The awk runs great if it is a pre-defined file that is used,... (7 Replies)
Discussion started by: cmccabe
7 Replies

3. Shell Programming and Scripting

Creating a script requiring a pause for user input

Hi I'm trying to create a basic script that pauses for user input to verify a file name before generating the output. I have numerous SSL certificate files which I am trying to determine the expiry date so what I'm trying to do is write a script so that is pauses to request the name of the .pem... (9 Replies)
Discussion started by: Buddyluv
9 Replies

4. Shell Programming and Scripting

Script interacts with user , based on user input it operates

i have a script which takes input from user, if user gives either Y/y then it should continue, else it should quit by displaying user cancelled. #!/bin/sh echo " Enter your choice to continue y/Y OR n/N to quit " read A if then echo " user requested to continue " ##some commands... (7 Replies)
Discussion started by: only4satish
7 Replies

5. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

6. Shell Programming and Scripting

Find ordinary files in directory input by user

I need to make a shell script that accepts a directory input by the user. The program searches for the directory and finds if it exists or not. Then if it does exist, it outputs the number of files within that directory. Here's what I have so far. result= echo "Please input a directory:... (5 Replies)
Discussion started by: itech4814
5 Replies

7. Shell Programming and Scripting

Counting the number of files within a directory input by the user

So I have a loop that stated if a directory exists or not. If it does it prints the number of files within that directory. I use this code... result=`(ls -l . | egrep -c '^-')` However, no matter which directory I input, it outputs the number "2" What is wrong here? (4 Replies)
Discussion started by: itech4814
4 Replies

8. Shell Programming and Scripting

Creating date directory and moving files into that directory

I have list of files named file_username_051208_025233.log. Here 051208 is the date and 025233 is the time.I have to run thousands of files daily.I want to put all the files depending on the date of running into a date directory.Suppose if we run files today they should put into 05:Dec:08... (3 Replies)
Discussion started by: ravi030
3 Replies

9. Shell Programming and Scripting

creating unique lists from user input

hi all, I'm trying to resolve a scenario where we prompt the user to enter 1 or more disk names. From there we would run a command on each disk which would give its location. This would allow us to create a list of disks at location A, a list of disks at location B,....etc... Any help... (1 Reply)
Discussion started by: annie
1 Replies

10. AIX

Limiting length of user in while creating user

Hi all, I am a newbe to aix 5.2. I want to specify the characters used by users while creating user in aix like specifying the length of the password should i use some sript for that if it is then please let me know how to do this if yes give me the link for the scripts. Thanks in advance ... (2 Replies)
Discussion started by: Satya Mishra
2 Replies
Login or Register to Ask a Question

Featured Tech Videos