Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux HostsFree MagazinesJobs
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Getting parameters from command line
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 07-24-2008   #1 (permalink)
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
kenshinofkin is offline   Reply With Quote
Old 07-24-2008   #2 (permalink)
Linux Enthusiast
 
Join Date: Nov 2007
Posts: 570
Lots of web resources - read this, you will be well on your way.
HROAdmin26 is offline   Reply With Quote
Old 07-24-2008   #3 (permalink)
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!
kenshinofkin is offline   Reply With Quote
Old 07-24-2008   #4 (permalink)
Linux Enthusiast
 
Join Date: Nov 2007
Posts: 570
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.
HROAdmin26 is offline   Reply With Quote
Old 07-24-2008   #5 (permalink)
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.
kenshinofkin is offline   Reply With Quote
Old 07-24-2008   #6 (permalink)
Linux Enthusiast
 
Join Date: Nov 2007
Posts: 570
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:

Quote:
./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:

Quote:
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.
HROAdmin26 is offline   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
 

Free Magazines
Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe
Systems Management News, the newspaper for IT systems administration and data center managers!
Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe
The Enterprise Newsweekly
eWeek is the essential technology information source for builders of e-business.
subscribe
Oracle Magazine
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe
Total Telecom
Total Telecom is "The Economist of the communications industry".
subscribe
More free magazines »



All times are GMT. The time now is 12:15 AM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.2.0