Question about a basic shell script: How to make sure it receives only one input?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Question about a basic shell script: How to make sure it receives only one input?
# 1  
Old 11-29-2011
Question about a basic shell script: How to make sure it receives only one input?

Hello all! I am very new to shell and Linux in general (I just started 2 days ago), I am trying to write a script that adds the size of the directories and files in a given directory and displays messages if the user puts in something wrong. I think I have covered all the possible problems except for one, I don't know how to make sure that the script checks that the user inputs only one directory and not more? If I try to run this program with 2 or more directories, then it runs the script for the first directory. How can I make the script to output a message saying that the user should specify only one directory to run the script on and not more?


Code:
#!/bin/sh
DIR="$1"
if [ $# -lt 1 ]
then
        echo "You haven't given any parameters, please give a directory and run the program in the following format: sh <programname.sh> <directory>"

elif [ ! -e "$DIR" ]
then
        echo "The directory you have specified does not exist."

elif [ ! -d $DIR ]
then
        echo "The parameter you have specified is not a directory, please specify a directory to run the program."
else
        du -s -m "$DIR"
fi


Thank you in advance for your help!
# 2  
Old 11-29-2011
Hi,

start reading bash man pages and the Advanced Bash Scripting Guide (you can find it on tldp.org), with regards to the while and read shell commands.

Then try to elaborate a solution of your own and come back here to ask for further help (trying to develop a solution of your own is always the best training way)

See ya
fra
# 3  
Old 11-29-2011
The below script will check if the parameters entered are more than 1 and if they're directories (files and non existing directories will be skipped). The sum of sizes of directory will be the output of the program. Is this what you're looking for?
Code:
#! /bin/bash

if [ $# -lt 1 ]
then
    echo "No parameters. Exiting."
    exit
fi

sum=0
for i in $@
do
    if [ -d $i ]
    then
        sum=$(($sum + `du $i | awk '{print $1}'`))
    else
        continue
    fi
done

echo $sum

# 4  
Old 11-29-2011
If you want only one argument, then you can do a check like this:
Code:
if [[ $# -ne 1 ]] ; then   #-ne  not equals
  echo "I need just 1 argument. No more, no less." 
  exit 1
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Basic Combination Shell Script

I need to have a script read a file that has a list of words in a single column like below:Black Blue Brown Orange Red Yellow Green White Purple Silver Grey Tan Then print to another file just all of the two-word possible combinations. Example: Black,Blue Anyone want to take a... (4 Replies)
Discussion started by: vespasian
4 Replies

2. UNIX for Dummies Questions & Answers

Basic loop awk/shell script question..

Hi, Sorry if this is a newbie question. I guess you can use either awk or shell script for this sequence of operations, but knowing very little about either of them I'm not sure how I should try to write this. The basic objective is to copy certain files that are scattered all over my... (10 Replies)
Discussion started by: pc2001
10 Replies

3. Shell Programming and Scripting

Deleting all the parallel processes launched when the main script receives a ctrl+c

Hi, I have a shell script that creates 2 parallel processes. When I press ctrl+c, i want the parallel process to get killed as well. #!/bin/bash cmd1="script1.py" cmd2="script2.py" ${cmd1} & pid1=$! echo ${pid1} ${cmd2} & pid2=$! (7 Replies)
Discussion started by: sana.usha
7 Replies

4. Shell Programming and Scripting

Basic question on shell script execution

I have two shell scripts in the different directories listed below, /root/dev/dir1/test.sh /root/dev/dir2/master.sh I am executing the master.sh script from the test.sh like below and getting 'Permission denied' error. #! /bin/sh #test.sh path='/root/dev' $path/dir2/master.sh But it... (2 Replies)
Discussion started by: vel4ever
2 Replies

5. Shell Programming and Scripting

Help! Basic shell script advice

##### (2 Replies)
Discussion started by: AidoPotato
2 Replies

6. Shell Programming and Scripting

Basic shell script help

Im trying to make a script that simply adds a word to the last available line in a txt file without overwriting any previous lines. Ive googled this and there are great examples but no one explain what each function does, and i dont entirely understand how it works. Basically Im looking for... (7 Replies)
Discussion started by: kylecn
7 Replies

7. Shell Programming and Scripting

Basic Shell Script Help

Lets say I wanted to create a script that would show what people are doing on my machine using the w command and refresh in about 6 seconds. What would be the easiest way to do this? I pretty much want the script to loop until I stop it. I'm using the BASH shell by the way. Help is appreciated.... (1 Reply)
Discussion started by: c4391
1 Replies

8. Shell Programming and Scripting

Basic script question

I'm trying to approach a problem but all I'm coming up with are complex ways to manipulate the data. But still not getting the desired outcome. directory of files.... file-100-foo file-100-man file-100-chu Need to copy the files and increment the number in the file name ... (4 Replies)
Discussion started by: suphawk
4 Replies

9. Shell Programming and Scripting

need a quick basic shell script help

im trying to run the below if command ifconfig -a |grep 10.100.120.21 gives me below output inet addr:10.100.120.21 Bcast:10.100.120.255 Mask:255.255.255.0 i just want a basic shell which says if above exists then continue how would i do this? (6 Replies)
Discussion started by: eb222
6 Replies

10. Shell Programming and Scripting

basic shell scripting question

If I did indeed grep something out of it, why woudln't $result show nothing? When I do $? , it does show success... What is the proper syntax so that $result shows actual thing it's grepping out? result=`(ssh $host tail -1 /something/somethingelse) | egrep -i "value" >dev/null` #echo... (3 Replies)
Discussion started by: convenientstore
3 Replies
Login or Register to Ask a Question