shell_06 - if the judge sentences

if statement:
 Use format if conditional statement:
 1, a single branch statements
    if condition; then
       execute the statement
    Fi
 2, double branch statements
    if condition; then
       execute statement 1
    the else
       execute statements 2
    Fi
 . 3, multi-branch statements
    if condition; then
       execute the statement . 1
    elif; the then
       execute the statement 2
    elif; the then
       executing the statement. 3
    the else
       execute the statement. 4
    Fi
 
Exit codes:
  Exit
  under certain conditions to determine if the condition is not satisfied, we have to manually exit the program, otherwise the code behind can not be executed;
  after the completion of the implementation of the code is correct, we have developed the correct exit code exit 0;
----------------------------------------------------------------------------
if statement examples:
1, to determine whether a file exists
#!/bin/bash
	# Determine whether a file exists
	if [ $# -lt 1 ]; then
		echo "At least one argument." 
		exit 1 
	be 

	if [ -e $1 ];then
		echo "The file exists"
	else
		echo "This file does not exist."
	if

2, determine whether the current system on the user's default shell bash whether the program, if there is, it shows how many such users, otherwise showed no such users; [and show those users is bash]

#!/bin/bash
	# Determine the user's default shell type
	
	declare -i sum=`grep "bin/bash$" /etc/passwd | wc -l`

	if grep "/bin/bash$" /etc/passwd &> /dev/null ; then
		echo "there is $ sum users, shell program to / bin / bash"
		grep "/bin/bash$" /etc/passwd | cut -d: -f1
		exit 0
	else
		echo "no such user"
		exit 1
	be

3, write a script, given a file, for example: / etc / inittab a, to determine whether there is a blank line in the file? b, if so, its line number blank lines displayed, otherwise there is no blank lines

	#^[[:space:]]*$

	#!/bin/bash
	#
	
	B=`grep -n "^[[:space:]]*$" /etc/inittab | wc -l`
	C=`grep -n "^[[:space:]]*$" /root/abc | cut -d: -f1`
	
	if [ $B -eq 0 ] ; then
		echo "no blank lines."
		exit 1
	else
		echo "there is a blank line, blank line behavior $ C"
		exit 0
	be 

4, write a script, given a user to determine whether the UID and GID, as if, like, it shows that the user is "good guy", otherwise it is "bad guy"

	#!/bin/bash
	#
	
	1, for doing traversal / etc / passwd
	for i in $ (cat / etc / passwd); do // sed be completed;
		2, is determined for each row UID and GID
		if [ `cut -d: -f3 $i` = `cut -d: -f4 $i` ];then
			echo "good guy"
			exit 0
		else
			echo "bad guy"
			exit 1
		be
	done

5, to determine whether the total entry command history command history is more than 1000, if greater than, "some command will gone" is displayed, otherwise OK

#!/bin/bash
	#
	S=`history | awk '{print $1}' | tail -1`
	
	if [ $S -gt 1000 ];then
		echo "some command will gone"
		exit 0
	else
		echo "OK"
	be

6, given a file, if an ordinary file, on the show, if the file is a directory, is also displayed, otherwise it displays "does not recognize"

#!/bin/bash
	#
	# input()
	read -t 5 -p ( "Please enter a file:") filename // -t waiting time
	echo # default to wrap

	if [ -n $filename ];then
		echo "eg. /etc/fstab"
		exit 8
	be

	if [ -f $filename ]; then
		echo "$ filename is a regular file."
		exit 0
	elif [ -d $filename ];then
		echo "$ filename is a directory file"
		exit 0
	else
		echo "does not recognize"
		exit 1
	be

7, write a script that can accept a parameter (file path) to determine if this parameter is an existing file on the show "ok", otherwise, it displays "No such file"

#!/bin/bash
	#
	
	read -p "Please enter a file path:" filename 
	
	if [ -e $filename ];then
		echo "OK"
	else
		echo "No such file"
	be

8, write a script, pass two parameters to the script, and display both the product and the two of

#!/bin/bash
	#
	echo $[$1+$2]
	echo $[$1*$2]

  

  

  

  

 

Guess you like

Origin www.cnblogs.com/lzqitdl/p/11365095.html