LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-14-2009, 12:44 PM   #1
Drigo
Member
 
Registered: Jun 2009
Posts: 111

Rep: Reputation: 17
How to pass arguments in a shell script?


Hello,
i am getting use to use shell scripts for linux.
How do I pass arguments?
Basically I found a shell where I can compare files and append the ones not found:

if ! diff file1 file2 > /dev/null; then
echo diff
else
echo not_diff
fi


But I want to replace file1 and file2 as two arguments...how can I do this? Thanks
 
Old 11-14-2009, 12:55 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

$1, $2, $3 etc represent the arguments give to the script at run time.

somescript.sh foo bar foobar

Inseide the somescript.sh script foo is assigned to $1, bar to $2, foobar to $3.

Although you can work with $1, $2 etc inside your script it is wise to use variables that are human readable.

Code:
#!/bin/bash

inFileOne="$1"
inFileTwo="$2"

if ! diff ${inFileOne} ${inFileTwo} > /dev/null; then
  <some code>
else
  <some other code>
fi
The above can be started as: scriptname.sh file1 file2

Do take a look at the Bash guides that are around. Here are 2:
Bash Guide for Beginners
Advanced Bash-Scripting Guide

Hope this helps.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C program to execute Shell script with arguments baigmd Programming 4 10-27-2009 10:51 PM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 02:45 AM
LXer: Parsing arguments for your shell script LXer Syndicated Linux News 0 07-18-2007 04:17 AM
Passing arguments to a shell script subu_s Programming 3 09-02-2005 06:13 AM
How to pass arguments from $prompt for php script ukjairaj Linux - Software 4 06-25-2004 12:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration