Writing Shell Management scripts (i)

Source: Internet
Author: User
Tags aliases

7.1 View a list of the shell programs available in the current Linux system
[Email protected] ~]# Cat/etc/shells
/bin/sh
/bin/bash
/sbin/nologin
/bin/tcsh
/bin/csh
/bin/ksh
7.2 Confirm some of the linked files that exist in the shell program
[Email protected] ~]# ls-l/bin/*sh
-rwxr-xr-x 1 root root 735004 2008-10-21/bin/bash
lrwxrwxrwx 1 root root 4 10-17 19:37/bin/csh-tcsh
lrwxrwxrwx 1 root root 10-17 19:37/bin/ksh-/etc/alternatives/ksh
lrwxrwxrwx 1 root root 4 10-17 19:35/bin/sh, bash
-rwxr-xr-x 1 root root 346756 2010-10-05/bin/tcsh
7.3 Switch to the shell environment of the TCSH program and verify that you can also use the Help internal directive and the external command ls
[Email protected] ~]# Ksh
# Help CD
Ksh:help:not found [no file or directory]
# ls-l Anaconda-ks.cfg
-RW-------1 root root 1286 10-17 19:46 anaconda-ks.cfg
# Exit//Return to the original bash environment
7.4 Log in to the system using a teacher user and change your login shell to tcsh
[[email protected] ~]$ grep "Teacher"/etc/passwd
Teacher:505:505::/home/teacher:/bin/bash
[Email protected] ~]$ Chsh
changing shell for teacher.
Password:
New Shell [/bin/bash]:/bin/tcsh
Shell changed.
[[email protected] ~]$ grep teacher/etc/passwd
Teacher:505:505::/home/teacher:/bin/tcsh
7.5 The Usermod command is used by the root user to re-modify the teacher user's login shell to "/bin/bash"
[Email protected] ~]# usermod-s/bin/bash teacher
[[email protected] ~]# grep "Teacher"/etc/passwd
Teacher:505:505::/home/teacher:/bin/bash
7.6 Use "!WH" to invoke the last command in the history command that starts with "WH" and execute it.
[email protected] ~]# which fdisk
/sbin/fdisk
[Email protected] ~]# pwd
/root
[Email protected] ~]#!WH
Which Fdisk
/sbin/fdisk
7.7 Viewing the last three History command records performed by the user
[Email protected] ~]# history | Tail-3
119 pwd
Which Fdisk
121 History | Tail-3
7.8 Execute the 119th command in the list of user history commands by the "! Serial number" form
[Email protected] ~]#!119
Pwd
/root
7.9 Set the number of bars for all user record history commands to 200
[Email protected] ~]# Vi/etc/profile
histsize=200
7.10 Set the number of bars recorded by the user Jerry's history command to 5, re-select to confirm the effect
[Email protected] ~]$ VI ~/.bash_profile
Export histsize=5
[Email protected] ~]$ history | Wc–l
5
7.11 log in with teacher user, execute "history–c" command has emptied its command history.
[Email protected] ~]$ history | Tail-2
cd
History | Tail-2
[Email protected] ~]$ history-c
[Email protected] ~]$ history
1 history
7.12 viewing all command aliases that have been set
[[email protected] ~]# alias
Alias cp= ' Cp-i '
Alias l.= ' ls-d. *--color=tty '
Alias ll= ' Ls-l--color=tty '
Alias ls= ' ls--color=tty '
Alias mv= ' Mv-i '
Alias rm= ' Rm-i '
Alias Vi= ' Vim '
Alias Which= ' Alias | /usr/bin/which--tty-only--read-alias--show-dot--show-tilde '
7.13 using the default alias "ll" to implement the function of "ls-l" command
[email protected] ~]# ll Install.log
-rw-r--r--1 root root 36318 10-17 19:46 Install.log
7.14 set a command alias Dir so that the "ls–ld–color=tty" command is executed and confirmed.
[[email protected] ~]# alias dir= '/bin/ls-ld--color=tty '
[[email protected] ~]# alias dir
Alias dir= '/bin/ls-ld--color=tty '
[Email protected] ~]# dir install.log
-rw-r--r--1 root root 36318 10-17 19:46 Install.log
7.15 cancels the dir command alias that was set in the previous step.
[Email protected] ~]# Unalias dir
[[email protected] ~]# alias dir
-bash:alias:dir:not found
7.16 cancels all command aliases that have been set in the user's current environment.
[Email protected] ~]# unalias-a
[[email protected] ~]# alias//will not find any alias information
7.17 View the identity information of the teacher user and save the output to the file "Idtch.txt".
[[email protected] ~]# ID teacher > Idtch.txt
[email protected] ~]# cat Idtch.txt
uid=505 (teacher) gid=505 (teacher) groups=505 (teacher)
7.18 View the identity information of the Jerry user and append to the file "Idtch.txt" without overwriting the original contents of the file.
[[email protected] ~]# ID Jerry >> idtch.txt
[email protected] ~]# cat Idtch.txt
uid=505 (teacher) gid=505 (teacher) groups=505 (teacher)
uid=507 (Jerry) gid=507 (Jerry) groups=507 (Jerry)
7.19 Save the initial password "123456" in the file "Pw.txt" and use the file to set the password for the Jerry user.
[Email protected] ~]# VI pw.txt
123456
[Email protected] ~]# passwd--stdin Jerry < pw.txt
Changing password for user Jerry.
Passwd:all authentication tokens updated successfully.
7.20 If you try to use the help command to view the assistance information for the LS command, the system will give an error because LS is not an internal command, and the error message can be redirected to the specified file via the "2>" operator.
[email protected] ~]# help LS 2> err.log
[email protected] ~]# cat Err.log
-bash:help:no Help topics match ' ls '. Try ' help ' or ' man-k ls ' or ' info ls '.
7.21 when you use the LS command to view the properties of the "/etc/password" file (which does not exist) and the "/etc/passwd" file, the output will contain both standard output and error output. Test the results of a separate redirect and merge redirection.
[Email protected] ~]# ls-l/etc/password/etc/passwd > Std.txt 2> err.log
[email protected] ~]# cat Std.txt
-rw-r--r--1 root root 2135 10-17 23:21/etc/passwd
[email protected] ~]# cat Err.log
LS:/etc/password: no file or directory
[Email protected] ~]# ls-l/etc/password/etc/passwd &> out.log
[email protected] ~]# cat Out.log
LS:/etc/password: no file or directory
-rw-r--r--1 root root 2135 10-17 23:21/etc/passwd
7.22 filter out lines that do not start with the "#" in the "/etc/inittab" file and filter out non-empty lines again, saving the results as "inittab.txt" files.
[Email protected]t ~]# grep-v "^#"/etc/inittab | Grep-v "^$" > Inittab.txt
[email protected] ~]# cat Inittab.txt
7.23 find the user account that uses bash as the login shell, the name begins with "Ha" in the system, and displays only the user name and the contents of the two fields of the login shell.
[[email protected] ~]# grep "Bash"/etc/passwd | grep "^ha" | Cut-d ":"-f 1,7
Handy:/bin/bash
Hackli:/bin/bash
7.24 view the total memory space of the current system, the remaining free memory space (MB), and remove other extraneous information.
[Email protected] ~]# Free-m | Head-2
Total used free shared buffers Cached
mem:1010 467 542 0 32 381
[Email protected] ~]# Free-m | grep "Mem" | awk ' {print $2,$4} '
1010 542
7.25 Create a new variable named "Day" with the initial content set to "Sunday".
[Email protected] ~]# Day=sunday
7.26 View the contents of the variable day, comparing the difference between using the "$" symbol and not using the "$" symbol.
[Email protected] ~]# echo Day
Day
[Email protected] ~]# echo $DAY
Sunday
7.27 immediately after the contents of the variable day is followed by the "Morningbus" string and displayed together.
[[email protected] ~]# echo $DAYMorningBus//Error reference
[Email protected] ~]# echo ${day}morningbus
Sundaymorningbus
7.28 confirm the contents of the variable day and use double quotation marks to assign the value ' today is $DAY ' to the Today variable.

[Email protected] ~]# echo $DAY
Sunday
[Email protected] ~]# today= "TODAY is $DAY"
[Email protected] ~]# echo $TODAY
Today is Sunday
7.29 confirm the contents of the variable day and use single quotation marks to assign the value ' today is $DAY ' to the Today variable.
[Email protected] ~]# echo $DAY
Sunday
[Email protected] ~]# today= ' TODAY is $DAY '
[Email protected] ~]# echo $TODAY
Today is $DAY
7.30 counts the number of users currently logged on to the local terminal (TTY1~TTY6) and saves the results to the variable usernum.
[Email protected] ~]# usernum= ' W | grep "TTY" | Wc-1 '
[[Email protected] ~] #echo $UserNum
3
7.31 use a single line of command to find out the name of the package that installed the FDISK program (the file location of the Fdisk program needs to be determined first).
[Email protected] ~]# RPM-QF ' which fdisk '
Util-linux-2.13-0.56.el5
7.32 use the Find command to find the files or directories left by the Hackli user in the system and delete them using the RM command.
[Email protected] ~]# rm-rf ' Find/home-user hackli '
7.33 the output from the previous example is saved to the variable fdiskpkg using a single line of command.
[[email protected] ~]# fdiskpkg=$ (RPM-QF $ (which fdisk))
[Email protected] ~]# echo $FdiskPKG
Util-linux-2.13-0.56.el5
7.34 Enter an entire row of data from the keyboard, assign a value to the variable helo, and confirm the contents of the HELO variable.
[email protected] ~]# Read HELO
Good morning, teacher!
[Email protected] ~]# echo $HELO
Good morning, teacher!
7.35 enter an entire row of data from the keyboard, assign to the variable G1, G2, and confirm the contents of the G1 and G2 variables.
[email protected] ~]# Read G1 G2
Good morning,teacher!
[Email protected] ~]# echo $G 1
Good
[Email protected] ~]# echo $G 2
morning,teacher!
7.36 read a numeric variable from the keyboard and give the corresponding prompt information.
[Email protected] ~]# read-p "Please input a number:" Yournum
Please input a number:501
[Email protected] ~]# echo $YourNum
30w
7.37 define a variable filesvr in the current shell environment, open a new child shell process (bash or zsh, etc.), and you will not be able to use the contents of the variable filesvr in a child shell environment.
[Email protected] ~]# filesvr=filesvr.benet.com
[Email protected] ~]# zsh
[Email protected] ~]# echo $FILESVR

[[Email protected] ~]# exit
[Email protected] ~]# echo $FILESVR
Filesvr.benet.com
7.38 confirm the contents of the variable filesvr and set it as a global variable and validate it in the child shell, the current shell environment.
[Email protected] ~]# echo $FILESVR
Filesvr.benet.com
[Email protected] ~]# export FILESVR
[Email protected] ~]# zsh
[[email protected] ~]# echo $FILESVR//The variable can also be used in a child shell environment
Filesvr.benet.com
[[Email protected] ~]# exit
[[email protected] ~]# echo $FILESVR//Return to the original shell environment can still use the variable
Filesvr.benet.com
7.39 Define two variables month, year, and set it as a global variable.
[Email protected] ~]# export Month=may year=2009
[Email protected] ~]# echo $MONTH $YEAR
May 2009
7.40 clears the variable you have set day, MONTH, year.
[Email protected] ~]# unset Day MONTH
[Email protected] ~]# ECHO $DAY $MONTH $YEAR
-bash:echo:command not found
7.41 set the value of the variable x, y to 34, 12, then the subtraction of the variable x, y and the result of the modulo operation.
[Email protected] ~]# expr $X + $Y
46
[Email protected] ~]# expr $X-$Y
22
[Email protected] ~]# expr $X \* $Y
408
[Email protected] ~]# expr $X/$Y
2
[Email protected] ~]# expr $X% $Y
10
7.42 calculates the value of the variable X with the number "123" and re-assigns the calculated result to the variable x.
[Email protected] ~]# echo $X
34
[[email protected] ~]# x= ' expr $X + 123 '
[Email protected] ~]# echo $X
157
7.43 Page view the shell variables that are set in the system environment.
[[Email protected] ~]# Set | Less
Bash=/bin/bash
Bash_argc= ()
bash_argv= ()
Bash_lineno= ()
Bash_source= ()
...//Omit more content
7.44 a semicolon-delimited display of the current user's user name, host directory, and login shell.
[Email protected] ~]# echo "$USERHOMESHELL"
Root:/root:/bin/bash
7.45 confirm the current command search path and add the "/opt/bin" directory to the existing search path.
[Email protected] ~]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
[Email protected] ~]# path= "/opt/binpath"
[Email protected] ~]# echo $PATH
/opt/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/ Root/bin
7.46 in the current user environment, change the value of the environment variable histsize used to limit the number of history command bars to 24.
[Email protected] ~]# history | Wc-l
229
[Email protected] ~]# export histsize=24
[Email protected] ~]# echo $HISTSIZE
24
[Email protected] ~]# history | Wc-l
24
7.47 edit the "~/.bash_profile" file and modify the path's settings so that the user can use the "/opt/bin" directory as the default search path after the next logon.
[Email protected] ~]# VI ~/.bash_profile
Path= $PATHHOME/bin:/opt/bin
Export PATH
7.48 verify the role of predefined variables, $$, $?.
[[email protected] ~]# Bash//open a new process
[[email protected] ~]# echo $ $$//show current process name, PID number
Bash 2040
[[email protected] ~]# Exitt//execute an error command
Bash:exxit:command not found
[[email protected] ~]# echo $? The return value is not 0, indicating that the previous command failed to execute
127
[[Email protected] ~]# exit
Exit
[[email protected] ~]# echo $? A return value of 0 indicates that the previous command executed successfully
0
7.49 confirm the number of system service script files and file types.
[Email protected] ~]# Ls-l/etc/rc.d/init.d/* | Wc-l
87
[Email protected] ~]# file/etc/rc.d/init.d/* | Tail-2
/etc/rc.d/init.d/ypbind:bourne-again Shell Script Text executable
/etc/rc.d/init.d/yum-updatesd:bourne-again Shell Script Text executable
7.50 View the Network Service script in the system at the beginning of the net file with five elements.
[Email protected] ~]# Cat/etc/rc.d/init.d/network | Head-5
#! /bin/bash
#
# Network bring Up/down Networking
#
# chkconfig:2345 10 90
7.51 Use the VI Editor to write a simple shell script file, repboot.sh, that reports the amount of space occupied by the "/boot" directory in the current system and lists the attribute information for the kernel file.
[Email protected] ~]# VI repboot.sh

~
~
#!/bin/bash
#To Show Usage Of/boot directory and mode of kernel file.
echo "Useage of/boot:"
Du-sh/boot
echo "The mode of kernel file:"
ls-lh/boot/vmlinuz-*
7.52 Add executable permissions for the script file repboot.sh written in the previous step.
[Email protected] ~]# ls-l repboot.sh
-rw-r--r--1 root root 164 10-18 08:02 repboot.sh
[Email protected] ~]# chmod a+x repboot.sh
[Email protected] ~]# ls-l repboot.sh
-rwxr-xr-x 1 root root 164 10-18 08:02 repboot.sh
7.53 executes the repboot.sh script file under the current directory.
[Email protected] ~]#./repboot.sh
Useage Of/boot:
5.9m/boot
The mode of kernel file:
-rw-r--r--1 root root 1.9M 2010-12-20/boot/vmlinuz-2.6.18-238.el5
7.54 Use the BASH interpreter program to manually load the statements in the Execute repboot.sh script file.
[Email protected] ~]# bash repboot.sh
Useage Of/boot:
5.9m/boot
The mode of kernel file:
-rw-r--r--1 root root 1.9M 2010-12-20/boot/vmlinuz-2.6.18-238.el5
7.55 using "." command to read the executable statement in the repboot.sh script file and run it in the current shell environment.
[Email protected] ~]#. repboot.sh
Useage Of/boot:
5.9m/boot
The mode of kernel file:
-rw-r--r--1 root root 1.9M 2010-12-20/boot/vmlinuz-2.6.18-238.el5

Writing Shell Management scripts (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.