Sunday, May 25, 2014

Unix Shell Built-in Commands(2)

1. alias unalias
./script_1:
 #! /bin/bash  
 func()  
 {  
   echo Hello world!  
   alias l='echo Hello world!'  
   return 0  
 }  

 func  
 l # it will output the "l: command not found"  
 #seems like we are not allowed to use alias at shell script  
 #and only usable at the shell  

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1  
 Hello world!  
 ./script_1: line 11: l: command not found  
==================================
Use alias at the terminal:
We are setting up l to represent "ls -lrt" in this case.
But note: such alias only works in curr shell, which is current process. If you open a new process(new shell), it won't work!

 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ alias l="ls -lrt"  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ l  
 total 40  
 -rw-rw-r-- 1 aubinxia aubinxia 8453 May 17 11:52 backup~  
 -rw-rw-r-- 1 aubinxia aubinxia  53 May 18 18:55 text~  
 -rw-rw-r-- 1 aubinxia aubinxia  0 May 20 22:33 backup  
 -rw-rw-r-- 1 aubinxia aubinxia  35 May 24 10:34 text  
 -rw-rw-r-- 1 aubinxia aubinxia  12 May 24 11:16 script?  
 -rw-rw-r-- 1 aubinxia aubinxia  12 May 24 11:16 script2  
 -rw-rw-r-- 1 aubinxia aubinxia  34 May 24 16:02 out  
 -rwxrwxr-x 1 aubinxia aubinxia  34 May 25 12:15 script_1~  
 -rwxrwxr-x 1 aubinxia aubinxia 234 May 25 12:23 script_1  
==================================
source file's function to set up alias:
./script_1 file is same as above
terminal:
1) First command run ./script_1, but unfortunately, alias doesn't work in script
2) Second command is trying to import ./script_1 to current shell, which make:
func() becomes one shell function!
And run "func" and "l" which output "Hello world!" separately!
3) After importing script_1, alias is already set up, we can use "l" to print out "Hello world!"
This example indicate that alias could only work in shell, and source just import everything from the ./script_1 and make them run at the current shell.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1  
 Hello world!  
 ./script_1: line 11: l: command not found  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ source ./script_1  
 Hello world!  
 Hello world!  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ l  
 Hello world!  

==================================
Use unalias to remove all alias:

unalias -a: can remove all alias in current process's environment
terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ alias l="echo hello world"  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ l  
 hello world  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ alias m="echo amazing world"  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ m  
 amazing world  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ unalias -a  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ l  
 ml: command not found  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ m  
 m: command not found  

===================================
Use unalias to remove single specified alias:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ alias l="echo Hello world!"  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ alias m="echo amazing world!"  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ l  
 Hello world!  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ m  
 amazing world!  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ unalias l  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ l  
 l: command not found  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ m  
 amazing world!  

2. umask
umask affect the default permission when creating file or folder in unix
terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ umask 0  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo Hello >t0  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ umask 777  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo Hello >t777  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ umask 1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo Hello >t1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ umask 2  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo Hello >t2  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ umask 3  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo Hello >t3  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ umask 4  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo Hello >t4  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ umask 8  
 bash: umask: 8: octal number out of range  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ umask 10  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo Hello >t10  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ls -lrt t*  
 -rw-rw-rw- 1 aubinxia aubinxia 6 May 25 13:42 t0  
 ---------- 1 aubinxia aubinxia 6 May 25 13:42 t777  
 -rw-rw-rw- 1 aubinxia aubinxia 6 May 25 13:42 t1  
 -rw-rw-r-- 1 aubinxia aubinxia 6 May 25 13:42 t2  
 -rw-rw-r-- 1 aubinxia aubinxia 6 May 25 13:42 t3  
 -rw-rw--w- 1 aubinxia aubinxia 6 May 25 13:42 t4  
 -rw-rw-rw- 1 aubinxia aubinxia 6 May 25 13:43 t10  

From above examples, we can see that the number specified by umask will turn off the corresponding bit. But the execution permission is one exception, it is always off when creating the file.
Note: umask has to provide one octal number, so "umask 8" is wrong, the correct way is "umask 10".

No comments:

Post a Comment