By: Dorris in Linux Tutorials on 2011年01月15日 [フレーム]
In Linux shell script, the if statement is used for conditional branching based on the result of a test command. The basic syntax for the if statement is as follows:
if [ condition ] then # commands to execute if condition is true else # commands to execute if condition is false fi
Here, condition is the test command whose exit status determines whether the condition is true or false. The test command can be any command that returns an exit status of 0 for success and a non-zero value for failure.
Some common test commands that can be used in the if statement include:
[ expression ]: tests a condition based on the values of its operandstest expression: same as above, can also be written as [[ expression ]]command: tests the exit status of a command[[ -n string ]]: tests whether a string is non-empty[[ -z string ]]: tests whether a string is emptyHere is an example of using the if statement with the [ expression ] test command:
#!/bin/bash if [ -f "/etc/passwd" ] then echo "File /etc/passwd exists" else echo "File /etc/passwd does not exist" fi
This script checks if the file /etc/passwd exists and prints a message accordingly. If the file exists, the condition is true and the echo command in the then block is executed. If the file does not exist, the condition is false and the echo command in the else block is executed.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Linux )
make: Nothing to be done for `all'.
Running jar files in background in ssh window
How to burn your CD / DVD ISO image using Nero Burning ROM (Ahead Software) on Windows
smskannel SMS gateway run in background
Can't locate ExtUtils/MakeMaker.pm in @INC ...
Could not open '': No such file or directory at lib/ExtUtils/MM_Unix.pm line 2697
Install and configure Memcached in linux
How to burn your CD / DVD ISO image using Media Creator (Adaptec/Roxio) on Windows
How to burn your CD / DVD ISO image using Nero Express (Ahead Software) on Windows
Latest Articles (in Linux)
smskannel SMS gateway run in background
Running jar files in background in ssh window
Install and configure Memcached in linux
Can't locate ExtUtils/MakeMaker.pm in @INC ...
Could not open '': No such file or directory at lib/ExtUtils/MM_Unix.pm line 2697
make: Nothing to be done for `all'.
How to burn your CD / DVD ISO image using Nero Burning ROM (Ahead Software) on Windows
How to burn your CD / DVD ISO image using Media Creator (Adaptec/Roxio) on Windows
How to burn your CD / DVD ISO image using Nero Express (Ahead Software) on Windows
How to burn your CD / DVD ISO image using k3b on CentOS
smskannel SMS gateway run in background
Running jar files in background in ssh window
Install and configure Memcached in linux
Can't locate ExtUtils/MakeMaker.pm in @INC ...
Could not open '': No such file or directory at lib/ExtUtils/MM_Unix.pm line 2697
make: Nothing to be done for `all'.
How to burn your CD / DVD ISO image using Nero Burning ROM (Ahead Software) on Windows
How to burn your CD / DVD ISO image using Media Creator (Adaptec/Roxio) on Windows
How to burn your CD / DVD ISO image using Nero Express (Ahead Software) on Windows
How to burn your CD / DVD ISO image using k3b on CentOS
© 2023 Java-samples.com
Tutorial Archive: Data Science React Native Android AJAX ASP.net C C++ C# Cocoa Cloud Computing EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Trends WebServices XML Office 365 Hibernate
Latest Tutorials on: Data Science React Native Android AJAX ASP.net C Cocoa C++ C# EJB Errors Java Certification Interview iPhone Javascript JSF JSP Java Beans J2ME JDBC Linux Mac OS X MySQL Perl PHP Python Ruby SAP VB.net EJB Struts Cloud Computing WebServices XML Office 365 Hibernate