Shell Script Simple command-line python runner

                 Recently I have been watching some Shell Script videos from youtube. I just wanted to know how SH works and interact with user. But finally ended coding a simple shell script program for successfully executing python programs. Here I tried to execute php codes using my sh file, but still could't find a solution.
You can download full program from My github.
First I will explain how the program flows.

First user enters he/she desired file name, shell script creates an empty text file with that name. Then program asks what kind of file is this (python or php).

When user enters appropriate file type,  changes the file extension from text to user desired file type.
Asks user to enter the program and save it using CTRL+D.
Finally tries to execute the program. (tries because only python codes are executed successfully in this)

Here we go, I will explain line by line, what is happening behind the scene (as I understood).

          echo "Please enter file name : "

This outputs the given string. Just like php's echo.

           read filename

This reads the user input value and saves it in the variable called filename. Remember when you declare SS variable for the first time, no need to use $variable.

          touch $filename

This creates an empty text file and name it with user's input. Look at here, when we are using the variable, so must use $variable.

           py="python" 
           php="php"
These two also variable declarations.

          echo "please enter your file type, python or php"
           read extension

Here user is asked to enter the file type, and that is saved in a variable called extension. Here comes the tricky parts. No doubt that you are familiar how to code if-else control structure. But SH has completely different syntax. It took me almost an hour to get this if loop working.

          if [ "$extension" = "$py" ];then

SS doesn't use ( ) for wrapping if statement, it uses [ ] , which we normally use when we are coding arrays. Pretty weird coding :D
If you are familiar with php, then you must think "$extension" this will be just a string.And the other most important thing. You must keep a space between [ and "$ and at the ending ].

if [ "$extension" = "$py" ];then  an another weird coding :D. Here if [ ]; then means start the if statement from here. We can use,
             if [ ]
           then              
This works the same way.

Then we need to change the file extension to desired language. To do it we use. This keep the original text file and make a new one to the current working directory with .py extension.

          mv $filename $filename.py

This can be used to move files for one directory to another.  mv $filename /folder1/folder2/$filename.py 

          cat > $filename.py

This means that, concatenate user inputs and send them to the $filename.py file. If you use cat < $filename.py , it will just print the file content.

          clear
This just clear the previous commands from terminal view and gives us a clean terminal.
   
          python $filename.py

This is executing the python file and print the result. I explained the portion for python code executing. If the user enters php as the file extension. Then

          elif [ "$extension" = "$php" ];then 

get executed, check whether the given extension is same as varible $php. This also weird coding, elif [ ]; then

You must use fi at the end of the if loop. its like the closing } in php. 
Enjoy coding and make sure you are not making mistakes I made during thing coding.

P.S : I tried to embedded a syntax highlighter to this, but wasn't working. So I will keep trying for that coding task too :)





Comments

Popular posts from this blog

Youtube-dl cheatsheet

Git mistakes - 1

Docker Development - Mistakes 1