Shell

shell基础

2018-10-15  本文已影响23人  柒月的天空

Linux学习

一、shell介绍
二、命令历史
三、命令补全和别名
四、通配符
五、输入输出重定向

一、shell介绍

二、命令历史

操作
[root@localhost ~]# echo $HISTSIZE
1000
[root@localhost ~]# 

[root@localhost ~]# vim /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}


if [ -x /usr/bin/id ]; then
if [ -z "$EUID" ]; then
    # ksh workaround
    EUID=`/usr/bin/id -u`
    UID=`/usr/bin/id -ru`
fi
USER="`/usr/bin/id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi

# Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
fi

HOSTNAME=`/usr/bin/hostname 2>/dev/null`
HISTSIZE=5000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi

for i in /etc/profile.d/*.sh /etc/profile.d/sh.local ; do
if [ -r "$i" ]; then
    if [ "${-#*i}" != "$-" ]; then 
        . "$i"
    else
        . "$i" >/dev/null
    fi
fi
done

unset i
unset -f pathmunge
[root@localhost ~]# 

三、命令补全和别名

操作
[root@localhost ~]# alias restartnet=-'systemctl restart network.service'
[root@localhost ~]# alias off='init 0'
[root@localhost ~]# 

[root@localhost ~]# !v
vim .bashrc 

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
alias off='init 0'
alias restartnet='systemctl restart network.service'
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
[root@localhost ~]# source .bashrc         //即时生效

[root@localhost ~]# ls /etc/profile.d/
256term.csh  bash_completion.sh  colorgrep.sh  colorls.sh  lang.csh  less.csh  sh.local  vim.sh      which2.sh
256term.sh   colorgrep.csh       colorls.csh   csh.local   lang.sh   less.sh   vim.csh   which2.csh
[root@localhost ~]# 

四、通配符

操作
[root@localhost ~]# ls *.txt
11.txt  12.txt  1a.txt  1.txt  22.txt  2a.txt  2.txt  a.txt  b.txt
[root@localhost ~]# 

[root@localhost ~]# ls *.txt
11.txt  12.txt  1a.txt  1.txt  22.txt  2a.txt  2.txt  a.txt  b.txt
[root@localhost ~]# ls ?.txt
1.txt  2.txt  a.txt  b.txt
[root@localhost ~]# ls ??.txt
11.txt  12.txt  1a.txt  22.txt  2a.txt
[root@localhost ~]# ls [0-9].txt
1.txt  2.txt
[root@localhost ~]# ls [0-9,a-z].txt
1.txt  2.txt  a.txt  b.txt
[root@localhost ~]# cat 1.txt > 2.txt 
[root@localhost ~]# cat 1.txt >> 2.txt 
[root@localhost ~]# ls aaa.txt 2>err
[root@localhost ~]# ls aaa.txt 2>>err
[root@localhost ~]# wc -l < 1.txt 
171
[root@localhost ~]# command > 1.txt 2>&1
[root@localhost ~]# wc -l 1.txt 
0 1.txt
[root@localhost ~]# 

五、输入输出重定向

操作
root@localhost ~]# !w
wc -l 1.txt 
19 1.txt
[root@localhost ~]# cat 1.txt >2.txt   //重定向
[root@localhost ~]# wc -l 2.txt 
19 2.txt
[root@localhost ~]# 
[root@localhost ~]# cat 1.txt >> 2.txt 
[root@localhost ~]# cat 1.txt >> 2.txt 
[root@localhost ~]# cat 1.txt >> 2.txt 
[root@localhost ~]# cat 1.txt >> 2.txt   //追加重定向
[root@localhost ~]# wc -l 2.txt 
95 2.txt
[root@localhost ~]#

[root@localhost ~]# ls *.txt 123.sh >b.txt 2>a.txt
[root@localhost ~]# cat b.txt 
11.txt
123.sh
12.txt
1a.txt
1.txt
22.txt
2a.txt
2.txt
a.txt
b.txt
[root@localhost ~]# cat a.txt 
[root@localhost ~]# 

[root@localhost ~]# wc -l < 1.txt 
19
[root@localhost ~]# wc -l 1.txt 
19 1.txt
[root@localhost ~]# less -n2 < 1.txt 
上一篇下一篇

猜你喜欢

热点阅读