How to check on parameters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check on parameters
# 1  
Old 05-20-2011
How to check on parameters

Hi

I have a script that reads a directory name, and then prints all de items in that directory. Now that works, but I want to check on the parameters to be sure that the user has write just 1 parameter, and if not, than the script closes.
# 2  
Old 05-20-2011
Code:
[ $# -eq 1 ] || {
  printf 'you must provide exactly %d parameter\n' 1
  exit 1
  }

# 3  
Old 05-20-2011
I'm doing this, but it gives an error.

Here's the script:

Code:
function listDirectory 
{
    echo "Give in a directory name"

    read name

    if [ $# -eq 1 ]; then

        if [ -d "$name" ]; then
            echo "Directory exists. Content: "
            ls "$name"
        else
            echo "Directory doesn't exist."
            exit
        fi
    else
        echo "More than one param. "
        exit
    fi

}
listDirectory

# 4  
Old 05-20-2011
$# inside your function is not the same scope as $# of the shell itself.

If you pass the shell arguments to your function, the $# check inside the function should work ...

where you call listDirectory
call it this way:

listDirectory $@
# 5  
Old 05-20-2011
If you are already expecting a command line parameter, then you should not prompt for it in the shell and vice-versa.
# 6  
Old 05-20-2011
Ok, now I'm doing it this way, but the other problem is listing the given directory. In my previous script, I had the variable $name, now I havent a variable, so how can I list the content of the given directory without a variable?

Here's the script:

Code:
#!/bin/bash

if [ $# -eq 1 ]; then

        if [ -d  ]; then
            echo "Correct. Content of the directory:"
            echo
            # HERE I WANT TO LIST THE CONTENT OF THE DIRECTORY
        else
            echo "Directory doesn't exist."
              exit
        fi
else
      echo "More than 1 param."
fi

# 7  
Old 05-20-2011
Code:
[ -d "$1" ] ...

...

Code:
ls -l -- "$1"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do i check if all parameters are set in bash?

Hi, I have 4 parameters passed to my shell and i validate if all four are entered using the following snippet: if then echo "Not entered" else echo "entered" fi I get the following output as 'Not entered even when i enter the values for all prompts. Please advise. Thanks. (5 Replies)
Discussion started by: Jesshelle David
5 Replies

2. Shell Programming and Scripting

Perl code to check date and check files in particular dir

Hi Experts, I am checking how to get day in Perl. If it is “Monday” I need to process…below is the pseudo code. Can you please prove the code for below condition. if (today=="Monday" ) { while (current_time LESS THAN 9:01 AM) ... (1 Reply)
Discussion started by: ajaypatil_am
1 Replies

3. Shell Programming and Scripting

check parameters

hi all, i write a script in ksh but i have a problem I want to check the number of parameters while ] do echo "Inserisci la macchina che vuoi restorare o scrivi fine per terminare lo script, puoi restorare" $a "client: " read CLIENT echo $CLIENT the variable... (1 Reply)
Discussion started by: FrancescoIt
1 Replies

4. UNIX for Advanced & Expert Users

Check EOF char in Unix. OR To check file has been received completely from a remote system

Advance Thanks. (1) I would like to know any unix/Linux command to check EOF char in a file. (2) Or Any way I can check a file has been reached completely at machine B from machine A. Note that machine A ftp/scp the file to machine B at unknown time. (5 Replies)
Discussion started by: alexalex1
5 Replies

5. Shell Programming and Scripting

How to check that passed parameters all have the same extension?

$ ls monkey.txt banana.csv tree.txt $ myscript monkey.txt tree.txt All extensions ARE alike. $ myscript *txt All extensions ARE alike. $ myscript monkey.txt banana.csv All extensions are NOT alike. $ myscript * All extensions are NOT alike. My brain has given up; what's the simplest... (11 Replies)
Discussion started by: cs03dmj
11 Replies

6. UNIX for Dummies Questions & Answers

check parameters

Hi, I have a script that takes two parameters. I would check the script to run if these parameters are to null or come with the correct data. If those parameters are to null, display a message, so if they come with two facts that are incorrect. As you might see that? Thx (1 Reply)
Discussion started by: pepeli30
1 Replies

7. AIX

tuning network parameters : parameters not persist after reboot

Hello, On Aix 5.2, we changed the parameters tcp_keepinit, tcp_keepintvl and tcp_keepidle with the no command. tunrestore -R is present in inittab in the directory /etc/tunables we can clearly see the inclusion of parameters during reboot, including the file lastboot.log ... (0 Replies)
Discussion started by: dantares
0 Replies

8. UNIX for Dummies Questions & Answers

Parameters to check while differentiating two servers

Hi All, I have two solaris servers. Please tell me what all parameters i can check to find out the difference between two servers. how to differentiate based on H/W,S/W etc like i have two servers spdwa013 $ uname -an SunOS spdwa013 5.8 Generic_117350-61 sun4u sparc SUNW,Sun-Fire-480R ... (1 Reply)
Discussion started by: usha rao
1 Replies

9. Shell Programming and Scripting

awk parameters check

Is there a way to preform check if the parameters that was send with the command awk -f `file_name.awk` `input_file` before even it gets to the BEGIN section (i have tested a try to check in the BEGIN it doesn't let ,you must make it on the section that after the BEGIN) and then decide if the... (1 Reply)
Discussion started by: tal
1 Replies

10. UNIX for Dummies Questions & Answers

Script to check for a file, check for 2hrs. then quit

I wish to seach a Dir for a specific file, once the file is found i will perform additional logic. If the file is not found within two hours, i would like to exit. Logically, I'm looking for the best way to approach this Thanks for any assistance in advance. Note: I'm using a C shell and... (2 Replies)
Discussion started by: mmarsh
2 Replies
Login or Register to Ask a Question