LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to pass arguments in a shell script? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-pass-arguments-in-a-shell-script-769126/)

Drigo 11-14-2009 11:44 AM

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

druuna 11-14-2009 11:55 AM

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.


All times are GMT -5. The time now is 06:30 AM.