The java classes are using sockets, so there is a client.java file and a server.java file, each with their own shell script. I also want to handle the command line arguments within the shell script, not the java classes.
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.
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 shellmyScript.sh : Linux shell script10 : First command line parameter accessible by $1rahul : Second command line parameter accessible by $2tecadmin.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.
![Script Script](https://i.stack.imgur.com/Uce1M.png)
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.
Java and the Mac OS X Terminal
This page is obsolete.
This document instructs you on how to use the Mac OS X Terminal with Java.
You will use the Java compiler javac to compile your Java programs andthe Java interpreter java to run them.To verify that Apple's implementation of Java 2 Standard Edition (Java SE 6) isalready installed:
- Run Software Update.
- Run Applications/Utilities/Java/Java Preferencesand verify that the Java SE 6 - 64-bit entry is checked andfirst in the list; if not, drag to change the preferred order.
You will type commands in an application called the Terminal.
- Open a terminal window. You can find this underGo -> Applications -> Utilities. Drag the Terminal to your dock sinceyou will be using it frequently.
- You should now have a Terminal window somewhere on the screen.It will have a prompt that looks something like:
- To check that you are running the right version of Java, typethe commands in boldface below. You should see something similar to the information printed below. The importantpart is that it says 1.6 or 1.5 (and not 1.4).
Then type
- Since you will be using the Terminal frequently, you may want tocustomize the default window settings (e.g., Monaco 13pt font with antialiasing).
- To check that you are running the right version of Java, typethe commands in boldface below. You should see something similar to the information printed below. The importantpart is that it says 1.6 or 1.5 (and not 1.4).
You will use the javac command to convert your Java program into a form moreamenable for execution on a computer.
- From the Terminal, navigate to the directory containing your .javafiles, say ~wayne/introcs/hello, by typing the cd commandbelow.
- Assuming the file, say HelloWorld.java is in the currentworking directory, type the javac command below to compile it.
If everything went well, you should see no error messages.
- Assuming the file, say HelloWorld.java is in the currentworking directory, type the javac command below to compile it.
You will use the java command to execute your program.
- From the Terminal, type the java command below.If all goes well, you should see the output of the program -Hello, World.
Input and Output |
If your program gets stuck in an infinite loop, type Ctrl-c to break out.
If you are entering input from the keyboard, you can signifyto your program that there is no more data by typingCtrl-d for EOF (end of file).You should type this character on its own line.
When I try to run java I get: Exception in thread 'main' java.lang.NoClassDefFoundError.First, be sure that HelloWorld.class is in the current directory.Be sure to type java HelloWorld without a trailing .classor .java.If this was not your problem, it's possiblethat your CLASSPATH was set by some other program so that it no longerincludes the current working directory.Try running your program with the command line
If this works, your classpath is set incorrectly.I get the error 'class file has wrong version 50.0, should be 49.0' when I compilefrom the Terminal. What does this mean?It's probably because DrJava is configured to use Java 6.0 and and your Terminal is configured to use Java 5.0.To change the default version of Java in your Terminal, launchJava Preferencest. Drag the Java SE 6 - 64-bit entryto appear first.
How do I get the menu to display at the topof the screen instead of at the top of the frame?Execute with java -Dapple.laf.useScreenMenuBar=true
![Script Script](/uploads/1/2/5/6/125607058/770461183.png)
Where can I learn more about the command line?Here is a short tutorial on thecommand-line.