Skip to content

23 de novembro de 2009

Using conditional expressions to improve your shell scripts

You can use these very simple shell script conditional expressions to improve your home-made scripts, making them more efficient and easy to mantain.

To check if a file exists and it’s executable, use:

#!/bin/bash
FILE=/usr/bin/passwd

if [ -x $FILE ];
then
 echo "File $FILE exists and it is executable"
else
 echo "File $FILE does not exists or it isnt executable"
fi

There are several other options. Here’s the full available conditional expressions:

-e: Returns true value if file exists
-f
: Return true value if file exists and regular file
-r: Return true value if file exists and is readable
-w: Return true value if file exists and is writable
-x: Return true value if file exists and is executable
-d: Return true value if exists and is a directory

Have fun improving your scripts !

Leia também:

  1. Teste: Mod Syntax Highlighting
  2. Como remover apenas um grupo de usuário no Unix
  3. Faça seu cron calar a boca (e deixar de entupir sua mailbox)
  4. Dia de grandes atualizações – Firefox, PHP e VirtualBox
  5. A Sysadmin’s Universal Translator (Rosetta Stone)
  6. Liberando root no Motorola Milestone 2.0
  7. YASG (Yet Another Security Guide)
  8. Liberar root no Milestone – firmware 2.1

Share your thoughts, post a comment.

(required)
(required)

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments