-->

Beginner Linux Scripting Mistakes

Beginner Linux Scripting Mistakes - Linux scripting is a fantastic way to automate tasks and even create some cool command line-based games. Bash hackers around the world often post some clever scripts to try out but before you go copying and pasting them, it’s worth highlighting some of the common mistakes.


BASH HACKERS


Even bearded seasoned Bash programmers make a mistake from time to time – including the non-bearded ones. Being able to eliminate the common errors saves a lot of time when scripting.


HASH-BANG

Forgetting to add the #!/bin/bash, the Hash-Bang, is one of the most common mistakes to make for the beginner scripter. The Hash-Bang is the interpreter that tells the system what shell to use and that what you’re running is in fact a shell script.

Beginner Linux Scripting Mistakes Hash-Bang
Beginner Linux Scripting Mistakes Hash-Bang


DOT SLASH


Remember, Linux scripts don’t run when you type the name of the file in. If you simply enter MyScript into the Terminal, it will attempt to execute a built-in command called MyScript – which doesn’t exist. Instead, you need to place ./ at the start: ./MyScript.

Beginner Linux Scripting Mistakes Dot Slash
Beginner Linux Scripting Mistakes Dot Slash


PERMISSIONS

The second most common issue for beginners, when their script doesn’t want to work, is that they’ve forgotten to give it executable permissions. After you’ve created your script you need to chmod +x it to allow the system to flag the file as executable. Once it’s an executable you can then run the script.

Beginner Linux Scripting Mistakes Permissions
Beginner Linux Scripting Mistakes Permissions


WHITESPACES

Most of us automatically continue writing script code without entering whitespaces after a variable but if you’ve been using other programming languages then it can become habit to hit the space bar frequently. In scripting, there’s no spaces on either side of the equals sign for variables. So, word = Hello is wrong, whereas word=Hello is correct.

Beginner Linux Scripting Mistakes Whitespace
Beginner Linux Scripting Mistakes Whitespace


COPYING

Setting a script that copies files from one place to another, such as a backup script, can be tricky. The part that stumps most folks is the naming of some files. If you set variables as the file and target, you need to encapsulate them in quotes. This way any whitespaces and extensions are considered. Such as, cp -- “$file” “$target”.

Beginner Linux Scripting Mistakes Copying
Beginner Linux Scripting Mistakes Copying


CHECK SPELLING

It’s all too easy to mistype a command in the Terminal. When you do it in a script though the end result can be failure or something totally different. Before you save and execute the script, have a quick look through to check you’ve not mistyped a command.

Beginner Linux Scripting Mistakes Check Spelling
Beginner Linux Scripting Mistakes Check Spelling


WRONG OS

While frustrating to the coder, it’s always amusing to see someone who’s written a script and inserted a Microsoft command instead of a Linux command. Clearing the screen is popular, where in Linux you use clear, someone who’s got their DOS head on uses cls.

Beginner Linux Scripting Mistakes Wrong OS
Beginner Linux Scripting Mistakes Wrong OS


CHECK FIRST

Always be cautious when copying scripts you’ve found online into your system and executing them. There are some Linux commands that kill your system beyond repair, forcing you to reinstall the OS. The rm -rf command, for example, wipes all the files and folders off your system. Always research script contents before executing.

Beginner Linux Scripting Mistakes Check First
Beginner Linux Scripting Mistakes Check First


MORE BEWARE

Following on from the previous common mistake, never blindly enter a website into a script or the Terminal that downloads and executes a script. There’s a good chance it could contain something malicious or wipe your files. The command wget http://somewebsite -O | sh downloads a script and automatically executes it.

Beginner Linux Scripting Mistakes More Beware
Beginner Linux Scripting Mistakes More Beware


MISSING OPERATORS

A popular mistake with most Bash scripters is missing out vital operators in their code. Missing quotations marks in an echo command, or square brackets when using loop, and even flags for external commands can have undesired results when you execute the script. Best to check through your code before running it.

Beginner Linux Scripting Mistakes Missing Operators
Beginner Linux Scripting Mistakes Missing Operators


0 Response to "Beginner Linux Scripting Mistakes"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel