The next section continues,
1. Reading input from the keyboard
#!/bin/bashread-p ' Please input something: ' Inputecho ' your input: ' $input
Operating effect:
./read1.shplease Input Something:123your input:123
2. While Loop and Case branch
#!/bin/bashprintf ' \nplease input a number or character, press "Q" or "Q" to quit:\n ' while Truedo read reply case $reply in q| Q) echo ' bye! '; break;; #结束循环 [a-za-z]*] echo ' You input A character: ' $reply;; #如果输入的是纯英文字符 [0-9]*] echo ' You input a number: ' $reply;; #如果输入的是纯数字 esac printf ' go on ... \ n ' Done
Operating effect:
./read1.shplease input a number or character, press "Q" or "Q" to quit:123you input a number:123please go on ... qweyou in Put a character:qweplease go on ... qbye!
3. File Operation related
3.1 File permissions and owner
LL (2 small Letter L) to list all files (and directories) details for the current directory
[Email protected] myservice]$ lltotal 4drwxrwxr-x. 2 Deploy deploy 6 Jan 17:10 a-rw-r--r--. 1 root root 14:41 test.txt
Read these output, the first 10 characters, split it, in the form of:
Type (1-bit) User rights (3-bit) owning user group permissions (3-bit) other group permissions (3-bit)
So:
d rwx rwx r-x says this is a directory (1th is the first letter of the directory), then the owning user has read (R,read's initials), writes (the first letter of the W,write), executes (x, represents execute) permissions, The owning user group also has all permissions, and the other user groups have read and Execute permissions.
-rw-r--r-- that this is a normal file (the 1th bit is-, the normal file), then the owning user has read, write permissions, the user group and other groups can only read.
Note: The 1th bit, in addition to D and-outside, and L means link, indicating that this is a symbolic link, and B is a block device, c means the character (character) device
Continue to look at the back of the output, the following two strings, that is, deploy deploy represents the user and the owning user group, in other words, directory A belongs to user deploy and user group deploy, and Test.txt belongs to user root and user group root
You can modify the file owner by Chown , Example:
sudo chown deploy test.txt
Indicates that the owning user of the Test.txt is modified to deploy, because the command requires higher permissions, so the front plus sudo switches to root identity, after execution, LL detect
[Email protected] myservice]$ lltotal 4drwxrwxr-x. 2 Deploy deploy 6 Jan 17:10 a-rw-r--r--. 1 Deploy root + Jan 14:41 test.txt
You can see that Test.txt's owning user has become a deploy.
chmod for modifying user and directory permissions, example:
[Email protected] myservice]$ chmod +w test.txt[[email protected] myservice]$ lltotal 4drwxrwxr-x. 2 Deploy deploy 6 Jan 17:10 a-rw-rw-r--. 1 Deploy root 14:41 test.txt
chmod +w test.txt indicates that the permissions of the owning user and the owning user group of the test.txt are increased by the write (w,write) permission
chmod a+w test.txt[[email protected] myservice]$ lltotal 4drwxrwxr-x. 2 Deploy deploy 6 Jan 17:10 a-rw-rw-rw-. 1 Deploy root 14:41 test.txt
This time I added an ato +w, which means all, which adds Test.txt write permission to everyone.
[Email protected] myservice]$ chmod u-w test.txt[[email protected] myservice]$ ll test.txt-r--rw-rw-. 1 Deploy root 14:41 test.txt
Guess, you can probably know the meaning of u-w, U is the user abbreviation,-means to remove permissions, so u-w means to remove the Write permission of the user, similar
[Email protected] myservice]$ chmod g-r test.txt[[email protected] myservice]$ ll test.txt-r---w-rw-. 1 Deploy root 14:41 test.txt
G-r indicates that the Read permission for the owning user group is removed.
In addition, from the binary system inside the computer, the permissions can be expressed in 3-bit 2, from left to right: Read, write, execute, so 111 means all permissions, 101 is read and execute permissions, 000 means no permissions, and then consider the user, the user group, other groups, that is, 3 sets of binary, So
chmod a+rwx can be simplified to chmod 777
Note: 777 is a 10 binary representation, converted to a permission 2 binary, i.e. 111 111 111
[Email protected] myservice]$ chmod 777 Test.txt[[email protected] myservice]$ ll test.txt-rwxrwxrwx. 1 Deploy root 14:41 test.txt
Similarly, if you want to remove all permissions from everyone
[Email protected] myservice]$ chmod-777 Test.txt[[email protected] myservice]$ ll Test.txt----------. 1 Deploy root 14:41 test.txt
3.2 File Test
#!/bin/bash[-F "test.txt"] && Echo ' test.txt is exists ' [!-f ' abc.txt '] && echo ' abc.txt is not exist '
The above paragraph indicates that if the file Test.txt exists, the output test.txt is exists, similarly, if the file abc.txt does not exist, the output is Abc.txt
Add some more judgments:
#!/bin/bash[-F "test.txt"] && Echo ' test.txt is exists ' [!-f ' abc.txt '] && echo ' abc.txt is not exist ' [-X "Test.txt"] | | Echo ' Test.txt can not execute ' [-d ' a '] && echo "A is directory" [-D "a"] && [-R "a"] && Echo "A is directory and can read"
The complete file judging criteria are collected as follows:
-e filename true if filename exists (E is the first letter of exist)-D filename If filename is a directory, True (d is the first letter of directory)-f filename If filename is a regular file, True (f is the first letter of file)-l filename If filename is a symbolic link, true (L is the first letter of link, note that this is uppercase)-r filename If filename is readable, true (R is the first letter of Read)-W filename If filename is writable, True (W is the first letter of write)-x filename if filename is executable, true (x is execute)-s filename If the file length is not 0, True (can be remembered as substance meaning)-h filename If the file is a soft link, true filename1-nt filename2 if filename1 is newer than filename2, then true (-nt can be remembered as new than) Filename1-ot filename2 If filename1 is older than filename2, then true (-nt can be remembered as old than)
4. Get output from other commands
cmd_pwd= ' pwd ' cmd_date= ' date "+%y-%m-%d%h:%m:%d" ' Current_dir=${cmd_pwd}current_time=${cmd_date}
Note: Instead of single quotes at both ends of the command string, ' (ie: the ~ key under the ESC key at the top left of the keyboard)
Output:
Current directory:/users/yjmyzz current time: 2016-01-21 18:13:21
5. Get the process ID of the Nohup
Build a app-test.sh to simulate the application with the following content:
#!/bin/bashecho ' I want to sleep second ' sleep 100
This piece of code, do nothing, come on sleep 100 seconds
Build another app-run.sh to simulate background startup App-test, as follows:
#!/bin/bashecho ' app is starting ... ' nohup./app-test.sh >/dev/null 2>&1 &echo ' ok,the pid is: ' $!
Test it:
? ~ ./app-run.shapp is starting...ok,the pid is:3168? ~ ps-ef|grep app-test.sh 501 3168 1 0 6:26pm ttys001 0:00.01/bin/bash ./app-test.sh
namely: $! The PID that can be taken to the program that Nohup background starts
Take this knowledge from the previous to a comprehensive exercise:
#!/bin/bashcmd_pwd= ' pwd ' pwd_dir=${cmd_pwd}pid_file=${pwd_ dir}/my.pid# start Sub_start () {printf "Starting ..." if [-W "${pid_file}"]; then echo ${pid_file} ' have already exists ' else nohup./netty-sample >/dev/null 2>&1 & pid=$! Echo $! > ${pid_file} sleep 2 printf ' ok!\n ' fi} #停止sub_stop () {printf ' stopping ... ' if [-W ' ${pid_fil e} "]; Then Pkill-f ${pid_file} sleep 2 printf ' ok!\n ' else printf ' \ n ' ${pid_file} ' not exists ' fi } #查看状态sub_status () {If [-W ' ${pid_file} '], then printf ' running.\n ' else printf ' not running.\n ' fi}c ASE $ in start) Sub_start;; stop) sub_stop;; Restart) printf "restarting...\n" sub_stop sleep 1 Sub_start sleep 1 sub_status; status) Sub_status;; *) printf "Usage: $ {start|stop|restart|status}\n" exit 1;; Esacexit 0
This is a generic startup shell script, the main idea is to launch the specified application (the above code, the application to be enabled for Netty-sample, you can modify it yourself), the first to find the process ID, and then saved to the PID file, so that after the detection of the PID exists, To determine whether the program is running or to end the program or to restart the program.
6. Write your own Linux service
Linux under the service, as long as the /etc/rc.d/init.d/ a own shell script can be, the reference content is as follows:
#! /bin/bash# chkconfig:2345 90# description:myservice ..... /etc/init.d/functionsdesc= ' my-service ' cmd= ' date ' +%y-%m-%d%h:%m:%s ' case ' $ ' in start ' printf '%s is starting ... ' ${desc}sleep 1printf ' ok!\n ' echo ${cmd} >>/opt/myservice/test.txt ; Stop) printf '%s is stopping ... ' ${desc}sleep 1printf ' ok!\n ' ;; Status) printf '%s is ok!\n ' ${desc};; Restart|reload|force-reload) stop$0 start; *) echo $ "Usage: $ start|stop|status|restart|reload|force-reload}" exit 2esacexit 0
Code is not complex, start in the specified directory to write a file, save it as myservice.sh, give execute permission, should be able to service MyService Start|stop|restart|status, very simple.
(Note: 3rd, 4 lines of comments can not be deleted, or later added to boot when the error will be.) )
If you want the MyService service to boot automatically, you can perform
sudo chkconfig--add myservice
Once added, you can try restarting the machine to see if the files in the specified directory are generated, and if new content is generated and written, the service is actually running.
In addition: Chkconfig does not add any parameters, you can see which services are currently configured to start the boot automatically. Chkconfig--del MyService If you want to remove it from the list of services that are started on boot.
Bash/shell Programming Learning (3)