Handy and very useful Linux and Unix shell script that reduce PDF file size using Ghostscript (gs) without using in third party sites.
Source: Linux shell script to reduce PDF file size – nixCraft
Handy and very useful Linux and Unix shell script that reduce PDF file size using Ghostscript (gs) without using in third party sites.
Source: Linux shell script to reduce PDF file size – nixCraft
In the course of my career, I’ve periodically come across code like this in shell scripts:
TEMP_FILE=/tmp/tempfile
Or sometimes, slightly more elegantly:
TEMPFILE=/tmp/tempfile.$$
The problems with the first example are obvious, especially if it appears in many different scripts. The second is better. The “$$” means “my process ID”, who if whatever script had a process ID of 5309, the TEMPFILE variable would be set to /tmp/tempfile.5309. This makes collisions between scripts extremely unlikely, but is still suboptimal. What if there is a file called /tmp/tempfile.5309 and it’s owned by another user, or what if you don’t have permission to write to /tmp? It’d be better to find out immediately than many lines later when you try to write something. …
Source: Here Today, Gone When You Exit: Proper Tempfiles in Shell Scripts – LowEndBox
You can write a bash script such that it receives arguments specified when the script is called from the command line. This method is used when a script has to perform a slightly different function depending on the values of input parameters (the arguments).
For example, you may have a script called “stats.sh” that performs a particular operation on a file, such as counting its words. If you want to be able to use that script on many files, it is best to pass the file name as an argument, so that you can use the same script for all the files to be processed.
Source: How to Pass Arguments to a Bash-Script (Lifewire)
Your Own Linux..! recently published a multi-part series on Unix/Linux shell scripting, that would be useful to anyone desiring to write a bash script. The articles are:
Introduction – Linux / Unix Shell (Bash) Scripting
Bash Scripting Variables – Environment and Special Shell Variables
Bash Scripting – Arithmetic, Logical, Relational and Bitwise Operators
Bash Scripting – Arrays with Examples
Bash Scripting Conditionals – if-then-else, if-elif-else, Nested if-else
Bash Scripting – ‘case’ Statement
Bash Scripting – ‘select’ Loop and ‘select-case’ Statement
That’s the complete list as of the publication date of this post (EDIT: Plus a couple of late additions). If you want to check to see if any have been added since then, go here.
Have you ever wanted to run bash script as a root user during startup? Here’s how you can harness the power of cron and set your scripts to run at startup.
Source: How to Run Bash Script as Root During Startup on Linux – Make Tech Easier
We’ve shown you how to use SSH to transfer files securely. But there’s a major issue with SSH’s default behaviour. You are only connected with the remote machine after you’ve manually entered the password which rules it out for any tasks you want to run unattended. Or does it?
Here’s a quick lowdown on how the OpenSSH CLI tools (scp and sftp) work so that you can better appreciate the issue. When you want to copy over files to or from the remote host, you can use scp which automatically initiates a SSH connection to the remote host. Every time you run a scp command it establishes a new connection to the remote. So if you have multiple scp commands you’d be entering the same password several times.
This is why you wouldn’t want to use scp in any scripts you want to run unattended. There’s also the fact that if you have multiple accounts on several machines on the network, you’d have trouble memorizing unique, strong passwords for each.
To overcome this problem, you need to switch OpenSSH’s default authentication mechanism to a key-based system.
Full article here:
Run Automated Scripts Over SSH (Make Tech Easier)
Here is something we could have used a few times in the past, had we known it existed:
These are answers to frequently asked questions on channel #bash on the freenode IRC network. These answers are contributed by the regular members of the channel (originally heiner, and then others including greycat and r00t), and by users like you. If you find something inaccurate or simply misspelled, please feel free to correct it!
All the information here is presented without any warranty or guarantee of accuracy. Use it at your own risk. When in doubt, please consult the man pages or the GNU info pages as the authoritative references.
BASH is a BourneShell compatible shell, which adds many new features to its ancestor. Most of them are available in the KornShell, too. The answers given in this FAQ may be slanted toward Bash, or they may be slanted toward the lowest common denominator Bourne shell, depending on who wrote the answer. In most cases, an effort is made to provide both a portable (Bourne) and an efficient (Bash, where appropriate) answer. If a question is not strictly shell specific, but rather related to Unix, it may be in the UnixFaq.
This FAQ assumes a certain level of familiarity with basic shell script syntax. If you’re completely new to Bash or to the Bourne family of shells, you may wish to start with the (incomplete) BashGuide.
If you can’t find the answer you’re looking for here, try BashPitfalls. If you want to help, you can add new questions with answers here, or try to answer one of the BashOpenQuestions.
Link: BASH Frequently Asked Questions
Over 100 questions are answered here. And in case you missed it above, this page only shows the questions – you have to click on the links that follow each question to see the answers.
People often don’t realize how powerful BASH really is. Very often people will try to write a program in a higher level language to do a simple task that could easily be done entirely in BASH. Look this page over and you might realize that you can do a lot more with BASH than you thought. And BASH is available in every version of Linux we’ve ever encountered (note we did not say “installed by default”, though it often is nowadays). Even small computers such as Raspberry Pis will usually have BASH installed as part of the operating system.
Recent Comments