shell if 参数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell if 参数相关的知识,希望对你有一定的参考价值。

1.如果a>b且a<c,在SHELL里怎么写?是if [ a>b -a a<c ]吗?
2.如果a>b或a<c,在SHELL里怎么写?是if [ a>b -o a<c ]吗?
3.如果a>b或(a<c且a<d)怎么写呢?"-o"是or的意思,"-a"是and的意思吗?
4."||"和"&&"在SHELL里可以用吗?也就是第一个写成if [ a>b && a<c ]也可以吗?
在SHELL的if条件里"-eq"和"=="
"-ne"和"!="
"-lt"和"<"等
是同一个意思吗?
这里看的越来越迷糊了..... 请哪位高手多指点指点!

不同的shell 有不同的写法,以下是各个shell的比较:
Feature
C/TC
Bourne
Bash
Korn

Variables:

Assigning values to local variables
set x = 5
x=5
x=5
x=5

Assigning variable attributes

declare or

typeset

typeset

Assigning values to environment variables
setenv NAME Bob
NAME='Bob'; export NAME
export NAME='Bob'
export NAME='Bob'

Read-Only Variables:

Accessing variables

echo $NAME

set var = net

echo $varwork

network

echo $NAME

var=net

echo $varwork

network

echo $NAME

var=net

echo $varwork

network

echo $NAME or print $NAME

var=net

print $varwork

network

Number of characters
echo $%var (tcsh only)
N/A
$#var
$#var

Special Variables:

PID of the process
$$
$$
$$
$$

Exit status
$status, $?
$?
$?
$?

Last background job
$! (tcsh only)
$!
$!
$!

Arrays:

Assigning arrays
set x = ( a b c )
N/A

y[0]='a'; y[2]='b'; y[2]='c'

fruit=(apples pears peaches plums)

y[0]='a'; y[1]='b'; y[2]='c'

set –A fruit apples pears plums

Accessing array elements
echo $x[1] $x[2]
N/A
echo $y[0] $y[1]
print $y[0] $y[1]

All elements
echo $x or $x[*]
N/A
echo $y[*], $fruit[0]
print $y[*], $fruit[0]

No. of elements
echo $#x
N/A
echo $y#[*]
print $#y[*]

Command Substitution:

Assigning output of command to variable
set d = `date`
d=`date`
d=$(date) or d=`date`
d=$(date) or d=`date`

Accessing values

echo $d

echo $d[1], $d[2],

...

echo $#d

echo $d
echo $d
print $d

Command Line Arguments (Positional Parameters):

Accessing

$argv[1], $argv[2] or

$1, $2 ...

$1, $2 ... $9
$1, $2, ... $10 ...
$1, $2, ... $10 ...

Setting positional parameters
N/A

set a b c

set `date`

echo $1 $2 ...

set a b c

set `date` or set $(date)

echo $1 $2 ...

set a b c

set `date` or set $(date)

print $1 $2 ...

No. of command line arguments

$#argv

$# (tcsh)

$#
$#
$#

No. of characters in $arg[number]
$%1, $%2, (tcsh)
N/A
N/A
N/A

Metacharacters for Filename Expansion:

Matches for:

Single character
?
?
?
?

Zero or more characters
*
*
*
*

One character from a set
[abc]
[abc]
[abc]
[abc]

One character from a range of characters in a set
[a–c]
[a–c]
[a-c]
[a–c]

One character not in the set

N/A (csh)

[^abc] (tcsh)

[!abc]
[!abc]
[!abc]

? matches zero or one occurrences of any pattern in the parentheses. The vertical bar represents an OR condition; e.g., either 2 or 9. Matches abc21, abc91, or abc1.
abc?(2|9)1
abc?(2|9)1

Filenames not matching a pattern
^pattern (tcsh)

I/O Redirection and Pipes:

Command output redirected to a file
cmd > file
cmd > file
cmd > file
cmd > file

Command output redirected and appended to a file
cmd >> file
cmd >> file
cmd >> file
cmd >> file

Command input redirected from a file
cmd < file
cmd < file
cmd < file
cmd < file

Command errors redirected to a file
(cmd > /dev/tty)>&errors
cmd 2>errors
cmd 2> file
cmd 2> errors

Output and errors redirected to a file
cmd >& file
cmd > file 2>&1
cmd >& file or cmd &> file or cmd > file 2>&1
cmd > file 2>&1

Assign output and ignore noclobber
cmd >| file
N/A
cmd >| file
cmd >| file

here document

cmd << EOF

input

EOF

cmd << EOF

input

EOF

cmd << EOF

input

EOF

cmd << EOF

input

EOF

Pipe output of one command to input of another command
cmd | cmd
cmd | cmd
cmd | cmd
cmd | cmd

Pipe output and error to a command
cmd |& cmd
N/A
N/A
(See coprocesses)

Coprocess
N/A
N/A
N/A
command |&

Conditional statement

cmd && cmd

cmd || cmd

cmd && cmd

cmd || cmd

cmd && cmd

cmd || cmd

cmd && cmd

cmd || cmd

Reading from the Keyboard:

Read a line of input and store into variable(s)

set var = $<

set var = 'line'

read var

read var1 var2...

read var

read var1 var2...

read

read -p prompt

read -a arrayname

read var

read var1 var2...

read

read var?"Enter value"

Arithmetic:

Perform calculation
@ var = 5 + 1
var=`expr 5 + 1`

(( var = 5 + 1 ))

let var=5+1

(( var = 5 + 1 ))

let var=5+1

Tilde Expansion:

Represent home directory of user
~username
N/A
~username
~username

Represent home directory
~
N/A
~
~

Represent present working directory
N/A
N/A
~+
~+

Represent previous working directory
N/A
N/A
~-
~–

Aliases:

Create an alias
alias m more
N/A
alias m=more
alias m=more

List aliases
alias
alias, alias -p
alias, alias –t

Remove an alias
unalias m
N/A
unalias m
unalias m

History:

Set history
set history = 25
N/A
automatic or HISTSIZE=25
automatic or HISTSIZE=25

Display numbered history list
history
history, fc -l
history, fc –l

Display portion of list selected by number
history 5
history 5

history 5 10

history –5

Re-execute a command

!! (last command)

!5 (5th command)

!v (last command starting with v)

!! (last command)

!5 (5th command)

!v (last command starting with v)

r (last command)

r5 (5th command)

r v (last command starting with v)

Set interactive editor
N/A (csh)

bindkey -v

or bindkey -e (tcsh)
N/A

set -o vi

set -o emacs

set -o vi

set -o emacs

Signals:

Command
onintr
trap
trap
trap

Initialization Files:

Executed at login
.login
.profile
.bash_profile
.profile

Executed every time the shell is invoked
.cshrc
N/A
BASH_ENV=.bashrc (or other filename) (bash 2.x)

ENV=.bashrc
ENV=.kshrc (or other filename)

Functions:

Define a function
N/A
fun() commands;
function fun commands;
function fun commands;

Call a function
N/A

fun

fun param1 param2 ...

fun

fun param1 param2 ...

fun

fun param1 param2 ...

Programming Constructs:

if conditional

if ( expression ) then

commands

endif

if ( command ) then

commands

endif

if [ expression ]

then

commands

fi

if command

then

commands

fi

if [[ string expression ]]

then

commands

fi

if (( numeric expression ))

then

commands

fi

if [[ string expression ]]

then

commands

fi

if (( numeric expression ))

then

commands

fi

if/else conditional

if ( expression ) then

commands

else

commands

endif

if command

then

commands

else

...

fi

if command

then

commands

else

...

fi

if command

then

commands

else

...

fi

if/else/elseif conditional

if (expression) then

commands

else if (expression) then

commands

else

commands

endif

if command

then

commands

elif command

then

commands

else

commands

fi

if command

then

commands

elif command

then

commands

else

commands

fi

if command

then

commands

elif command

then

commands

else

commands

fi

goto

goto label

...

label:

N/A
N/A
N/A

switch and case

switch ("$value")

case pattern1:

commands

breaksw

case pattern2:

commands

breaksw

default:

commands

breaksw

endsw

case "$value" in

pattern1) commands

;;

pattern2) commands

;;

*) commands

;;

esac

case "$value" in

pattern1) commands

;;

pattern2) commands

;;

*) commands

;;

esac

case "$value" in

pattern1) commands

;;

pattern2) commands

;;

*) commands

;;

esac

Loops:

while

while (expression)

commands

end

while command

do

command

done

while command

do

command

done

while command

do

commands

done

for/foreach

foreach var (wordlist)

commands

end

for var in wordlist

do

commands

done

for var in wordlist

do

commands

done

for var in wordlist

do

commands

done

until
N/A

until command

do

commands

done

until command

do

commands

done

until command

do

commands

done

repeat

repeat 3 "echo hello"

hello

hello

hello

N/A
N/A
N/A

select
N/A
N/A

PS3="Please select a menu item"

select var in wordlist

do

commands

done
参考技术A 1) bash
a=3 ; b=2 ; c=4
if (( a > b )) && (( a < c ))

或者

if [[ $a > $b ]] && [[ $a < $c ]]

或者

if [ $a -gt $b -a $a -lt $c ]

2) a=3 ; b=2 ; c=4

if (( a > b )) || (( a < c ))

或者
if [[ $a > $b ]] || [[ $a < $c ]]

或者
if [ $a -gt $b -o $a -lt $c ]

3) -o = or , -a = and , 但我一向只用 || 或者 &&
4) 可用, 但是要两个独立的 [ ] , [[ ]] 或 (( ))
看 1)
5) -ne 比较数字 (numberic) ; != 比较字符 (string), 但后者拿来
比较数字也可,只是不是标准用法
-lt 是等同 < , 但 < 只能在 shell 的数值操作符 (( )) 或
者 逻缉操作符 [[ ]] 才可使用, -lt , -eq , -gt , -ge
-le , 这些是 test , 就是 [ ] 这个内建命令使用的条件操
作符, 数字用, = , != 字符用, == 这个该是 [[ ]] 用的,
可用来比对正规表示式, 但用在 [ ] 也可,只是不太正统用法

这可以吗? 呵呵本回答被提问者和网友采纳

shell实现备忘录功能

shell实现备忘录功能

代码

write_file()
	#第一个参数为日期
	#第二个参数为文件名
	#第三个参数为时间
	#第四个参数为标题
	#第五个参数为内容
	if [ ! -d "./file" ]; then
		echo "create a file directory"
		sudo mkdir -m 777 file
	fi
	if [ ! -d "./file/$1" ]; then
		echo "create a $1 directory"
		sudo mkdir -m 777 ./file/$1
	fi
	# touch "./file/$1.txt"
	echo "writing....."
	echo "日期"
	echo $3 >>./file/$1/$2.txt
	echo "title"
	echo $4 >>./file/$1/$2.txt
	echo "content"
	echo $5 >>./file/$1/$2.txt


run()
	echo "----------备忘录--------------"
	echo "请选择功能:1.添加备忘录 2.查询备忘录 3.退出"
	read num
	if [ $num -eq 1 ]
	then
		add_file
	elif [ $num -eq 2 ]
	then
		select_file
	else
		echo "退出"
		exit 0
	fi



add_file()
	echo "------------添加备忘录------------"
	echo "Enter title:"
	read title
	echo "Enter content:"
	read content
	DATE=`date +%Y-%m-%d`
	TIME=`date +%Y-%m-%d,%H:%M:%S`
	echo $DATE
	name="$DATE-$title"
	echo $name
	write_file $DATE $name $TIME $title $content


select_file()
	echo "------------查询备忘录------------"
	echo "输入查询日期,例如1999-09-19"
	read D
	if [ -d "./file/$D" ]; then
		ls ./file/$D
		echo "查询结束"
	else
		echo "没有记录"
	fi


# 主程序运行
while :
do
	run
done

以上是关于shell if 参数的主要内容,如果未能解决你的问题,请参考以下文章

linux shell if 指令参数备忘

如何正确处理Shell 函数传人参数中包含的特殊字符

Shell与if相关参数

Shell编程之if简单判断两个数字大小

if语句与$特殊含义

shell实现备忘录功能