0

The requirement is to create a shell script that can create a FileName.java file where FileName would be the parameter passed to the script while running the script.

Example:

$ ./shellscript.sh Sample

On executing the above command, a file Sample.java should get created with content:

class Sample{
   public static void main(String[] args){
      for(int i=0;i<10;i++){
      System.out.println("Hello World !!!"); 
   }
}

If sources for learning bash scripting is added in the answer it would be very helpful.

2
  • See: tldp.org/LDP/abs/html Commented Jun 10, 2020 at 14:10
  • Thank you @stark, will definitely check Commented Jun 10, 2020 at 14:12

3 Answers 3

1

You just need to make a "template", something like:

classTemplate.txt

class #ClassName{
   public static void main(String[] args){
      for(int i=0;i<10;i++){
      System.out.println("Hello World !!!"); 
   }
}

After that, you just need to replace #ClassName with your parameter

#create .java file
cp classTemplate.txt classTemplate.java 
#set class name
sed -i 's/#ClassName/MyClass/g' classTemplate.java
Sign up to request clarification or add additional context in comments.

Comments

0

Here-docs will expand variables, so

NAME=$1
cat << EOF > $NAME.java
class $NAME{
   public static void main(String[] args){
      for(int i=0;i<10;i++){
      System.out.println("Hello World !!!"); 
   }
}
EOF

Comments

0

The following book is a good reference:

https://www.amazon.com/Bash-Pocket-Reference-Power-Admins/dp/1491941596

I would just continue to use stackoverflow and google. This is what has helped me most, since I can search for specific solutions to what I am trying to do. In stackoverflow, you can also run your code and proposed solution by other people and find improved solutions. This is extremely valuable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.