linux shell 之别名的使用

别名的基本格式: alias alias-name='original-command'
fg. alias dir=ls #将dir作为ls命令的别名
dir
anotherres.sh execout.sh hfile part3 sleep10.sh timedread.sh
a.out FILE1 indirectapp1.sh parttotal sleep55.sh timer1
append.sed FILE2 indirect.sh pass.awk sturecord timer1.c
AREACODE.db findphone.awk insert.sed PEO.dbaa subscol.sh timer2
argv.awk forever.sh llaa PEO.dbab subsenv.sh timer2.c
awk.dll.gz function10.sh llab PEO.dbac subsep.sh timer3
backls.sh function11.sh llac PROFESSOR.db subsig.sh timer3.cpp
calare.sh function13.sh loggg refor.sh subsparallel.sh traploop.sh
CARGO3.db function14.sh MapClearTest.class reif.sh subsvar.sh traverseFile
conlon.sh function1.sh MapClearTest.java REPEAT TEACHER1.db traverseFile.c
count_word.sh function2.sh modify.sed resshell.sh TEACHER.db vartype.sh
doubleparenthese.sh function3.sh mysql_test.c rewhile.sh TEACHER_HOBBY.db WORDLIST
e_sleep57.sh function4.sh newfile scr2.awk test1.c xaa
evalpos.sh function5.sh nokillme.sh scr.awk test1.cpp xab
evalre.sh function6.sh output selfkill.sh test.c xac
evalsource function7.sh outputnull sever.c testNet.c
execerr.sh function8.sh part1 sever.o test_sockpair
execin.sh function9.sh part2 sever.s test_sockpair.c

alias cdglo='cd /home/daheng'
cdglo
/home/daheng

copy llaa lla_bak

unalias copy #删除别名copy
copy llaa lla_bak
No command 'copy' found, did you mean:
Command 'copt' from package 'z88dk-bin' (universe)
Command 'hcopy' from package 'hfsutils' (main)
Command 'fcopy' from package 'fai-client' (universe)
Command 'bcopy' from package 'bareos-tools' (universe)
Command 'bcopy' from package 'bacula-sd-pgsql' (universe)
Command 'bcopy' from package 'bacula-sd-sqlite3' (universe)
Command 'bcopy' from package 'bacula-sd-mysql' (universe)
Command 'rcopy' from package 'rdmacm-utils' (universe)
Command 'mcopy' from package 'mtools' (main)
copy: command not found

unalias -a 删除所有的别名
vi alias.sh
#!/bin/bash

shopt -s expand_aliases #设置此选项后,脚本才可以使用别名功能

alias detail="ls -l" #双引号命令引起,也可以为它建产别名
detail /root/in* #别名后使用了通配符
echo
directory=/root/
prefix=in*
alias vardetail="ls -l $directory$prefix" #引号内加入了变量
vardetail

echo "Deleting all aliases:"
unalias -a #删除所有的别名

#测试是否成功删除别名
detail
vardetail
./alias.sh
ls: cannot access '/root/in*': Permission denied

ls: cannot access '/root/in*': Permission denied
Deleting all aliases:
./alias.sh: line 17: detail: command not found
./alias.sh: line 18: vardetail: command not found

alias命令不能在诸如if/then结构,循环和函数等混合型结构中使用

vi loopalias.sh
#!/bin/bash
shopt -s expand_aliases #开启expand_aliases选项

alias copy=cp #在循环体外设置别名copy
count=0

while : #while循环,冒号表示永为真
do
alias ipconfig=ifconfig #在循环体之内设置别名ipconfig
let count=count+1
if [ $count -ge 2 ] #若count大于或者等于2,刚进入if/then结构
then
echo "Using alias in if/then structure"
ipconfig
break
fi
echo "Using alias in while loop structure"
copy output outputnew
done
./loopalias.sh
Using alias in while loop structure
Using alias in if/then structure
./loopalias.sh: line 14: ipconfig: command not found

 

posted @ 2021-04-12 10:41  zhudaheng123  阅读(404)  评论(0)    收藏  举报