Find the answer to your Linux question:
Results 1 to 6 of 6
Hello all! I am new to the whole UNIX/Linux world and have a few questions about scripting. I am trying to create a script that takes two directories as parameters ...
  1. #1
    Just Joined!
    Join Date
    Jul 2008
    Posts
    5

    Getting parameters from command line

    Hello all! I am new to the whole UNIX/Linux world and have a few questions about scripting. I am trying to create a script that takes two directories as parameters and does different checks on the two. The first problem I am having is how do I pass these directories to the script. I have this so far:

    #!/bin/sh
    local dir1=$1
    local dir2=$2

    Not much so far but I want the script to be called like this
    compdir <dir1> <dir2>

    I created 2 directories named dir1 and dir2 and when I type $compdir dir1 dir2
    I get -bash: dir1: command not found. What am I doing wrong here? Another question I have is when I get the directories say I have foo(this is a file) and dirX in dir1 and I want to check and see if they are in dir2?

    Thanks

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    Lots of web resources - read this, you will be well on your way.

  3. #3
    Just Joined!
    Join Date
    Jul 2008
    Posts
    5
    Thanks for the resource but its a little much. I am looking through it but not seeing anything. If someone could just tell me what I am doing wrong when passing the directories that would help so much!

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    Your question implies you don't have the basics, which means you will be asking the next basic question after this one, and then another, and another...

    You may also want to post actual command execution and results, because I can't tell what you "ran" to get your results.

    If your script is compdir, then it must be executable and is run by either being in your PATH statement or called explicitly with:
    Code:
    ./compdir dir1 dir2
    Also, what happens if the dir names have spaces in them?

    It is also *fairly standard* convention to name Bash shell scripts with a .sh extension, which would make your script compdir.sh.

    This is all covered in the linked guide and reading/understanding it (not *scanning it*) will save you a lot of headaches.

    Is it a file or directory?

    String Comparison

    Edited: Removed variable reference.

  5. #5
    Just Joined!
    Join Date
    Jul 2008
    Posts
    5
    I have found most the commands I will need to use now its just setting up a script to take 2 directories. I *scanned it* looking for scripting help not file and directory help. The resource say nothing about "setting up" a script. Like I said I typed "$compdir dir1 dir2" with out quotes. I created compdir in my root folder using vi. I saved it as compdir. Now thinking about it, I created a text file right? If I want to create a script I have to have the .sh? The script needs to be called like this $compdir <dir1> <dir2>. I just want to know how to create a script and get directories from the command line. I will go away and then leave you alone after that.

  6. #6
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    I don't know where you are getting $script from and I've never seen that syntax to invoke a script.

    As mentioned, the script must be readable and executable by the user calling the script and then:

    If you are in the directory where the script resides:

    ./compdir dir1 dir2
    From anywhere on the system:

    Code:
    /home/me/compdir dir1 dir2
    If the script is in your PATH variable:

    Code:
    compdir dir1 dir2
    You don't *have* to have .sh on the scriptname, it is only *convention* to make scripts easy to identify AS scripts.

    More info from BASH guide:

    2.1. Invoking the script

    Having written the script, you can invoke it by sh scriptname, [1] or alternatively bash scriptname. (Not recommended is using sh <scriptname, since this effectively disables reading from stdin within the script.) Much more convenient is to make the script itself directly executable with a chmod.

    Either:

    chmod 555 scriptname (gives everyone read/execute permission) [2]
    or

    chmod +rx scriptname (gives everyone read/execute permission)

    chmod u+rx scriptname (gives only the script owner read/execute permission)

    Having made the script executable, you may now test it by ./scriptname. [3] If it begins with a "sha-bang" line, invoking the script calls the correct command interpreter to run it.

    As a final step, after testing and debugging, you would likely want to move it to /usr/local/bin (as root, of course), to make the script available to yourself and all other users as a systemwide executable. The script could then be invoked by simply typing scriptname [ENTER] from the command line.
    Notes
    [1]

    Caution: invoking a Bash script by sh scriptname turns off Bash-specific extensions, and the script may therefore fail to execute.
    [2]

    A script needs read, as well as execute permission for it to run, since the shell needs to be able to read it.
    [3]

    Why not simply invoke the script with scriptname? If the directory you are in ($PWD) is where scriptname is located, why doesn't this work? This fails because, for security reasons, the current directory (./) is not by default included in a user's $PATH. It is therefore necessary to explicitly invoke the script in the current directory with a ./scriptname.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...