I have written a Bash script which uses cases and functions for university, but no matter where I move the scripts, she says that they are not indented properly. What is the correct indentation of this sample of the script?
clear echo "Start A Process" echo "*************************" echo "1. Oxygen Level." echo "***************************" echo "2. Temperature ." echo "*************************" echo "3. Mars Rover Speed." echo "*************************" echo "4. Satellite Distance" echo "*************************" echo "5. Carbon Dioxide Level" echo "*************************" echo "6. Humidity %." echo "*************************" echo "Press A to quit." echo "*************************" read -p "Please make a choice:" choice2 #Choices case "$choice2" in 1) $script/simulate start oxygen echo "Oxygen Level Started." sleep 5 ;; 2)
Is there a website or a function on VMware where I can get my code automatically indented?
Advertisement
Answer
Indenting is done to clarify your code. Indentations are usually used for loops, if statements, function definitions to make it easy to see what statements are part of that loop or part of the if statement.
Another trick to make your code more readable is adding blank lines to separate out blocks of related code. For example, separating out your menu from the rest of your code with a couple of blank lines:
clear echo "Start A Process" echo "*************************" echo "1. Oxygen Level." echo "***************************" echo "2. Temperature ." echo .... #And on and on... echo "*************************" echo "6. Humidity %." echo "*************************" echo "Press A to quit." echo "*************************" read -p "Please make a choice:" choice2 case "$choice2" in... ...
This makes it easier to see the menu you’re presenting. You could also use a Here document to eliminate the repeating echoes and quotation marks that make your menu hard to see. I’d would eliminate the over fancy stuff like the lines of asterisks. They don’t add anything to the program and just make it harder to read – both code wise and execution wise:
clear cat <<MENU START A PROCESS ------------------ 1. Oxygen Level. 2. Temperature . 3. Mars Rover Speed. 4. Satellite Distance 5. Carbon Dioxide Level 6. Humidity %. A. Quit ------------------ MENU read -p "Please make a choice:" choice2 case "$choice2" in... ...
Notice how using a here document makes your menu is easy to see and format.
Finally, your case
statement should follow the indentation rule of keeping common lines indented, so you not only know the code that goes with the case
statement, but that you also indent each choice:
case "$choice2" in 1) $script/simulate start oxygen echo "Oxygen Level Started." sleep 5 ;; 2) ..... ..... ;; ..... esac
Some people will compact this a bit:
case "$choice2" in 1) $script/simulate start oxygen echo "Oxygen Level Started." sleep 5;; 2) ..... .....;; ..... esac
The indentation is pretty much the same. A few lines are combined to make the case
more compact and easier to read.
To help you with indentation and coding, use a program editor instead of just a text editor. A good program editor like VIM will automatically indent your code and even highlight the syntax making it easier to read. Eclipse is used for Java programming, but can be used as a text editor for shell scripts. A good editor can also help you with the way to use certain commands of statements by showing you the manpage for that command. When you press capital K
in VIM, it will show you the manpage for that command.
Bash program beautification is quite tricky. However, there is a Python program that will attempt the task.