A Command-line Arguments are passed after the name of a program in command-line operating systems like DOS or Linux and are passed into the program from the operating system. Shell scripts also accept command line arguments similar to nix commands. Command line arguments are useful for passing input to script at runtime which has its own advantage. This article will help you to pass command line arguments in a shell script.
In this Java tools tutorial, I will guide you how to use the java command provided in JDK (Java Development Kit) to execute Java programs. Table of content: Run a Java program from a.class file; Running a Java program from an executable jar file. You can compile any java source using javac in command line; eg, javac CopyFile.java. To run: java CopyFile. You can also compile all java files using javac.java as long as they're in the same directory. If you're having an issue resulting with 'could not find or load main class' you may not have jre in your path.
To pass a command line argument we can simply write them after script name separated with space. All command line parameters can be access by their position number using $. A sample example of passing command line argument to shell script.
sh: Linux shell
myScript.sh: Linux shell script
10: First command line parameter accessible by $1
rahul: Second command line parameter accessible by $2
tecadmin.net: Third command line parameter accessible by $3
Access Command Line Argument with Position Number
So as above example command line parameters are accessible at $1, $2, $3…. $9, $10…. $100 etc. Maximum length of command line parameters are not defined by shell but it’s defined by operating system and measured in Kilobytes.
$* – Store all command line arguments
[email protected] – Store all command line arguments
$# – Store count of command line arguments
$0 – Store name of script itself
$1 – Store first command line argument
$2 – Store second command line argument
$3 – Store third command line argument
..
$9 – Store 9’th command line argument
$10 – Store 10’th command line argument
…
…
$99 – Store 99’th command line argument
Example 1:
Create a shell script to Print all Argument with script name and total number of arguments passed. create script file myScript.sh suing following content.
Execute the script
Example 2:
Create loop through all arguments in shell script. To do this create a shell script file myScript2.sh suing following content.
Execute the script
Access Command Line Argument by Shifting Position
We can also access all command line arguments by shifting their position in a shell script. Like access your first command line argument with $1. Now shift arguments with 1. It means the second argument is now at first position, similarity third is at second and so on.
Create a shell script myScript3.sh using below content and execute with argument. Now watch how parameters are shifted using “shift <number>” command in a shell script.
Execute the script and closely observe the output of $1 in script.
I hope this will help you for understanding command line arguments in a shell script. For any suggestion please add your comments.
How To Use Command Line Arguments in Eclipse
LINKS INTO DOCUMENT BELOW
Introduction
Detailed Instructions using Welcome.java
Instructions using Args.java
Instructions using TempConversion.java
How To Use Command Line Arguments in Eclipse
The main example used in these notes is the Welcome class, developed in the WelcomeToJava Eclipse project.
This program accepts one input argument value, which is automatically assigned to the first value in String[] args, called args[0]. (Computer people tend to count from 0 and not 1.) Study the program for a few minutes to find where args[0] is used. Notice that it is of type String.
The detailed instructions are in the next section, complete with screenshots from the Welcome class in Eclipse.
Two other example programs are described in the last section.
Detailed Instructions for Command Line Arguments in Eclipse Example: Welcome.java
Basically, it's simple. Click on Run ->Run(not Run Last Launched).
In the window shown below, notice that the Welcome Java application is highlighted.
In the large subwindow of the Run window, there is a set of tabs, labelled Main, Arguments, JRE, etc. Notice that the Main tab is highlighted.
Click on the Arguments tab. This action brings up a window with a few places to enter values. You want to type in all your input values into the Program arguments window, with spaces (blank characters) between each value.
For the Welcome program, only one input argument value is needed: the first name of a new Java student. For this run, we have chosen Sarah as the name of this student. Notice where it has been typed in. This is where all command line arguments should be entered. These values remain the same for all runs, until they are changed in this same window.
Then just click Apply, followed by Run. The screenshot below shows the output in the console window.
Another Example of a Java program using Command Line Arguments:Args.java
Another example is the Args class, developed in the TestArgs Eclipse project.
This program requires more than one command line argument.
Click on Run ->Run(not Run Last Launched).
Click on the Arguments tab, in the large subwindow of the Run window. This action brings up the Program arguments window, where you should type in your input values.
For the Args program, eight input argument values are needed. For this run, we have chosen eight random sets of characters, some numeric, some alphabetic, as input values: namely, 101, Joe, Larry, 93214, 3.145, 66, ABCDEF, xxxxx. Notice that there is at least one space between every two arguments.
Then just click Apply, followed by Run. The screenshot below shows the output in the console window.
If you were to click on Run ->Run Last Launched at this point, the same command line arguments would be used automatically, producing the same output.
To change the value of the command line argument, you need to click on Run ->Run again, and change the argument(s) in the Program assignment window.
Further Example of a Java programs using Command Line Arguments:TempConversion.java
Yet another example is the TempConversionCL class, developed in the TempConversion Eclipse project.
This program requires only one command line argument, but this value must be converted from a String to a double value to be used arithmetically.
Again, click on Run ->Run(not Run Last Launched).
Click on the Arguments tab, in the large subwindow of the Run window. This action brings up the Program arguments window, where you should type in your input values.
For the TempConversionCL program, only one input argument value is needed: the value of the Celsius temperature. For this run, we have chosen 28.5 for this value of the Celsius temperature.
Then just click Apply, followed by Run. The screenshot below shows the output in the console window.