Linux shell script ‐f file test operator

If you're writing a linux shell script, or reading one, you might be wondering what the "-f" operator actually is, or why it doesn't work for you. This article describes about what the "-f" operator does in a linux shell script and also goes into detail about the other operators related to it.

Introduction to Linux shell script operators

There are three types of operators in shell script - file test operators, conditional operators and arithmetic operators.

File Test Operators - Check for the existence of a file (can be limited to a specific type of file such as directory, regular file, symbolic links, etc) or the properties/attributes of a file such as read/write permissions, file ownership information, etc. The "-f" operator is a file test operator.

What is "-f" operator?

The -f operator is used to check the existence of a regular file (Check out different file types of linux here). It does not check whether other types of files exist, such as directory, synbolic links. If you have to check whether the existence of such files, you can use "-b" (for block devices), "-c" (for character devices) and "-d" (for directories). Also, all these different kinds of files can be tested using the "-e" operator. The "-f" operator only checks whether the specified file is a regular file (neither a device file, nor a directory). So if you want to test the existence of a file, regardless of its type, then you can use "-e" operator. Bash shell also supports "-a" operator, which accomplishes the same as "-e" operator.

Lets look at the sample linux shell script, which uses different file operators such as "-f", "-e" and "-a" and the result of this script's execution under different shells:

Sample Script

Execution Result

Under bash shell

The file exists (-a operator)
Regular file exists (using -f operator)
Character special file exists (using -c operator)
Block special file exists (using -b operator)
Directory exists (using -d operator)
The file exists (-e operator)

As you can see, both the "-a" and the "-e" operators perform the same function under bash - check for the existence of any kind of file. The usage of "-f" in a bash shell script is deprecated.

Under dash shell

file-existence-check.sh: 1: [: -a: unexpected operator
The file does not exist (-a operator)
Regular file exists (using -f operator)
Character special file exists (using -c operator)
Block special file exists (using -b operator)
Directory exists (using -d operator)
The file exists (-e operator)

The dash shell does not support the usage of "-a" operator. So you can use "-e" operator in its place.

Under zsh shell

file-existence-check.sh:[:1: too many arguments
The file does not exist (-a operator)
Regular file exists (using -f operator)
Character special file exists (using -c operator)
Block special file exists (using -b operator)
Directory exists (using -d operator)
The file exists (-e operator)

Like dash, zsh too, does not support the usage of "-a" operator. Here too, only "-e" operator instead of "-a" operator

So, you can consider the "-e" operator to be the universal existence checker for all files. And "-f" operator just checks for the existence of a regular file. Thus, in your linux shell script, use "-f" to check whether a file exists and is just a regular file.

References

  1. General Overview of Linux Filesystem
  2. TLDP's Bash guide for Beginners

Linux Foundation's free course on Open Source Licenses

Have you had a hard time understanding the Open Source Licenses? Are you confused whether you can use a specific open source library in your work? Linux Foundation now offers a free course on Open Source Licenses and Compliance guidelines for such prominent licenses.

Why should we understand Open Source License?


Understanding how copyrights and licenses work, and being able to clearly and accurately specify them is key to making sure that your software is acceptable into upstream projects, and reflects your intentions correctly. Also, knowing what licenses come with libraries could help you choose the appropriate library for your software.

Linux Foundation Course on Open Source Licenses
Open Source License - Which to use?

About the course


The "LFC191 Compliance Basics For Developers" course is designed to help software developers and those involved in producing software that will be distributed to understand why it is important to add copyrights and licenses to their code, as well as how to do so.

The material in this course provides information on how to create file notices with copyrights and licenses to minimize problems and ambiguity, which is useful for internal corporate projects as well, as for contributing to open source projects. An overview of the types of licenses to consider, as well as the other permissions that may be needed to secure prior to external contribution, is also summarized.

The course offers insights about different kinds of licenses, when to use them, considerations while contributing to open source projects, etc..

Accessing the course


You have to register for the Linux Foundation's free course at Compliance Basics for Developers course page. This course is available free of cost. After registration, you can track your courses at Linux foundation though its portal page.

Follow us for more tips and tricks. See the right side pane for our social networking pages. Share your thoughts in comments.

How to run shell scripts on Android Devices

Have you ever wanted to be free from performing tedious tasks in Android? Have you ever wanted to write android shell scripts? Well you are not the only one. This article shows you how to write android shell scripts, which helps you in easing your jobs.

Android Shell Script

Uses for Shell Scripts in Android Devices


Writing the android shell scripts have various uses - taking backups of files that you save in android, archiving files stored by another application, etc. As for me, I had to rename the files and folders downloaded with a comic downloader manually and compress each folder individually. So I wrote a simple shell script, that renames the individual folders and creates archives of them.

Prerequisites for running Shell Scripts in Android Devices


If your goal is to write a shell script making use of comprehensive utilities (such as awk, find, etc.), see the Advanced Shell Scripting subsection. Or else, if you just want to write simple shell scripts, see the Simple Shell Scripting subsection.

Advanced Shell Scripting in Android

Since this method is aimed at providing comprehensive abilities for writing shell scripts (utilities such as awk, find, sort, etc.), this requires a rooted android device for this. Rooting an android device is an extensive process, and differs for different devices. How to root your android device is something that you will have to lookup on your own.

BusyBox and Terminal Emulator for Android Shell Script
Busy Box & Terminal Emulator for Android Shell Scripting


In your rooted android device, install an app called BusyBox. It provides most of the *nix utilities such as awk, sed, find, sort, head, etc., which will be useful when you try to write a shell script to run on Android. Then install Terminal Emulator (You can also use other similar tools, but I use Terminal Emulator).

BUSYBOX ON GOOGLE PLAY
TERMINAL EMULATOR ON GOOGLE PLAY

Simple Shell Scripting in Android


This method does not require to root your android device. Just the Terminal Emulator is required to run the shell scripts you created. Refer the link above for getting Terminal Emulator.

Writing Shell Scripts in Android


You can write Shell Scripts in your PC and transfer it to your Android device, or else, write them directly on your Android device. Start writing your android shell script by using the following as the first line (Also known as Shebang)

#!/system/bin/sh

Running Shell Scripts in Android


Then save your entire script and open Terminal Emulator. Now run the script using sh command.
sh <path to the script>/<script name>

Here is the script that I created:

#!/system/bin/sh
echo "Voila!! Shell script works on Android"

And here is what I get in the terminal emulator:

Shell Script in Android


Only for rooted devices: If you want the script to be run directly as a command, place the script in /system/bin (or) /system/xbin . Then make the file executable by running the following command:

su
mount -o rw,remount /system
cp <script_path>/<script_name> /system/bin/<script_name>
chmod 555 /system/bin/<script_name>

For example I have copied the script "testmsg.sh" to /system/bin, renamed it to "testmsg" using the following commands:

su/
mount -o rw,remount /system
cp /storage/sdcard0/sh/testmsg.sh /system/bin/testmsg
chmod 555 /system/bin/testmsg (chmod +x did not work, giving me a bad mode error)

Now in the Terminal Emulator, just enter the name of the file and the script will execute.

Shell Script as a command in Android

I hope that wasn't a long winded approach for writing the shell scripts in Android. If you've got other ways for attaining the same result, kindly share them in the comments.

Follow us for more tips and tricks. See the right side pane for our social networking pages.

Shell Script on Android
4 stars - "A very useful article." This article about running shell scripts on android was very useful.

Domain naming 101

What name is to a person, domain name will be for your web page, as simple as that. In other words you will need to put a lot of effort and thought in coming up with a proper name. Not to mention that a well-chosen name can do wonder for any SEO endeavor, or some other effort your page tries to make when it comes to promotion or marketing. With this in mind, here are some of the basic ideas that you need to consider before coming up with a final version of your site’s domain name.


Consider Keywords

First thing you need to have in mind is what kind of words and phrases people look for while browsing your niche. Try to compose a list of several of these keywords and start coming up with ways in which you could use them in your domain name. It would be a good idea to do a bit of a keyword search on your own. This step can be invaluable in your future endeavors to gain a better position on search engine results.

Being Creative

In order for your website to become truly memorable you need to come up with as creative name as possible. Not only is creative name more likely to draw attention but also more likely to be memorized. Because of this, it might be a good idea to find a domain that would allow you to play around with an extension like if it was a part of a word or a sentence. A perfect example of this is a domain.me which allowed as cunning and amusing names such as aweso.me and follow.me.

Keeping it short

Even though sometimes you may feel obliged to be descriptive in your name, it is always better to keep it short. Short of course means simple and memorable and not just abbreviated so that it will fit the form. If you aim for your page to take off and even for it to become a brand, what you need to do is create a name that is easy to remember, easy to pronounce and even easier to type.

Avoid numbers

Numbers in your name are what makes a lot of confusion and are as such quite controversial. First of all, even people who learn your domain name by heart will at times feel challenged to remember whether that seven in your name was written in letters or numerals. Same kind of inconvenience can also be caused by hyphens. Third time when this happens to them, confusion will be replaced by anxiousness and even annoyance. These two feelings are not something you want your site to be associated with.

Avoid copyright infringement

Seeing how it takes you only several seconds to check if there is a website with that name somewhere, there is really no excuse on this one. Even though this infringement is still somewhat controversial and tricky to prosecute, a thing like this is still theft of intellectual property. If nothing else happens, you do not want your business to be mistakenly associated with something completely unrelated. Especially if taken into consideration that this content may even be inappropriate to some of your target audience like in the problem Lego had a while back.

Go unique

Now, some black hat strategies resort to promotion via association. Here, you use someone else’s good reputation to promote your business. Although this may be a good short term move, in the long run it can make whole hell break loose. It won’t be long until this deception is noticed by someone of authority and then your page and domain name will be completely compromised. Even though the right way is sometimes a slower one, it is always worth taking.

As you can see, making a good domain name is not so hard after all. All you need to do is make it specific, relevant and easily memorable. Also, you need to make sure to exclude all elements that could complicate the entire process. Finally, all that will be left for you is to choose an appropriate domain and you will be set and ready.

How Magento Can Operate on Linux's Ubuntu 14.04

In the era of a computer-governed world, there is a growing number of commercial online services. Although most corporations are moving towards pay-per-service philosophy, we can still use free sources for our business and personal purposes. Probably the largest free of charge operating system in the world is Linux. Its open-code structure and ubiquitous presence have made it the most popular system for e-commerce. When it joins forces with Magento, we get a true IT mastodon; now let's learn the basics of installing Magento on Ubuntu 14.04.


Required preconditions

Since Magento is among the most popular ecommerce platforms in the world, there are certain requirements that need to be met before you can start your online ecommerce store. Firstly, you should ensure that there is enough space on your VPS for the data you will be dealing with. So, you should provide the minimum of 2 GB of RAM for your software bits. Secondly, you should install the LAMP crew on your VPS profile. If you need a reminder how to install Linux, Apache, My SQL and PHP on Ubuntu, check this video tutorial. Finally, you will have to create a sudo account, to be able to command the server setup as a non-root user.

PHP and Apache tweaking

In order to ensure that your ecommerce tasks are kept safe in the online environment, every Ubuntu and Magento user has to configure PHP and Apache. When it comes to Apache, it is necessary to organize your virtual host file, to enable Apache to supervise your Magento site in a proper way. To do that, it is necessary to create a new file to manage the virtual host role. Learn more details about setting Apache for Ubuntu and Magento here.
Of course, you also have to configure the amount of memory needed for PHP operations. Every script being executed on Apache is automatically given 128MB of memory by PHP. However, it might not be a sufficient amount of memory for all the scripts run during ecommerce tasks. To change the memory, run the PHP configuration file in Apache: sudo nano /etc/php5/apache2/php.ini. Then find the line that looks like this: memory_limit = 128M. The next step is changing the number to 512 M. It should give your ecommerce platform enough script space for the beginning of your business.


Rounding off the circle

Once the software prerequisites have been sorted out, you can download and install the Magento b2b platform. By following this link, you will get to the latest version. First you should download the necessary file directly to your home directory. Then you have to extract files. When you get a Magento directory inside your home dir, you need to transfer all the unpacked files to the document root in Apache. As you have moved them, you can delete the Magento folder from the home directory. The final step is finishing the installation process through the Magento Interface. 

By starting your ecommerce business through the use of these two systems, you will be safe from intrusions of all kinds. What is more, you can always upgrade or reduce your online store in accordance with your needs. To conclude, this combination of a free-source system and an ecommerce platform with a long tradition is a win-win choice for every new Internet trader.