您的位置:首页 > 运维架构 > Shell

shell script 学习之 bash设定文件分析

2013-09-13 22:01 375 查看
/etc/profile ---> ~/.profile ---> ~/.bashrc

==============================================

ting@ting-PC:~$ cat /etc/profile

# /etc/profile: system-wide .profile file for the Bourne shell (sh(1))

# and Bourne compatible shells (bash(1), ksh(1), ash(1), ...).

if [ "$PS1" ]; then # if [ $sting ] 如果string 非空,返回0 (true)

if [ "$BASH" ] && [ "$BASH" != "/bin/sh" ]; then

# The file bash.bashrc already sets the default PS1.

# PS1='\h:\w\$ '

if [ -f /etc/bash.bashrc ]; then # if [ -f file ] 如果文件存在

. /etc/bash.bashrc # . /etc/bash.bashrc 等同于 source /etc/bash.bashrc

fi

else

if [ "`id -u`" -eq 0 ]; then #``反单引号中是命令,id -u表示输出有效用户id

PS1='# '

else

PS1='$ '

fi

fi

fi

# The default umask is now handled by pam_umask.

# See pam_umask(8) and /etc/login.defs.

if [ -d /etc/profile.d ]; then # if [ -d ... ] 如果目录存在

for i in /etc/profile.d/*.sh; do # for in 语句与通配符和用!!!

if [ -r $i ]; then # if [ -r file ] 如果文件存在且可读

. $i

fi

done

unset i #unset!

fi

没有读~/.profile???奇怪,看来ubuntu又跟鸟哥的书不一样了

看~/.profile的说明就明白了:

# ~/.profile: executed by the command interpreter for login shells.

# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login

# exists.

===============================================================================

ting@ting-PC:~$ cat .profile

# ~/.profile: executed by the command interpreter for login shells.

# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login

# exists.

# see /usr/share/doc/bash/examples/startup-files for examples.

# the files are located in the bash-doc package.

# the default umask is set in /etc/profile; for setting the umask

# for ssh logins, install and configure the libpam-umask package.

#umask 022

# if running bash

if [ -n "$BASH_VERSION" ]; then #if [ -n $string ] 如果string 非空(非0),返回0(true)

# include .bashrc if it exists

if [ -f "$HOME/.bashrc" ]; then

. "$HOME/.bashrc" # . "$HOME/.bashrc" 等同于 source "$HOME/.bashrc"

fi

fi

# set PATH so it includes user's private bin if it exists

if [ -d "$HOME/bin" ] ; then

PATH="$HOME/bin:$PATH"

fi

=================================================================

转载别人总结的if 和 for in 用法
http://blog.sina.com.cn/s/blog_6151984a0100ekl6.html
条件表达式

文件表达式

if [ -f file ] 如果文件存在

if [ -d ... ] 如果目录存在

if [ -s file ] 如果文件存在且非空

if [ -r file ] 如果文件存在且可读

if [ -w file ] 如果文件存在且可写

if [ -x file ] 如果文件存在且可执行

整数变量表达式

if [ int1 -eq int2 ] 如果int1等于int2

if [ int1 -ne int2 ] 如果不等于

if [ int1 -ge int2 ] 如果>=

if [ int1 -gt int2 ] 如果>

if [ int1 -le int2 ] 如果<=

if [ int1 -lt int2 ] 如果<

字符串变量表达式

If [ $a = $b ] 如果string1等于string2

字符串允许使用赋值号做等号

if [ $string1 != $string2 ] 如果string1不等于string2

if [ -n $string ] 如果string 非空(非0),返回0(true)

if [ -z $string ] 如果string 为空

if [ $sting ] 如果string 非空,返回0 (和-n类似)

if command 等价于 command+if $?

[ ] && ——快捷if

[ -f "/etc/shadow" ] && echo"This computer uses shadow passwors"
http://blog.csdn.net/hainan16/article/details/6667483
for i in a b c #依次对i赋值a, b, c

for i in *.h #利用通配符遍历文件

test()

{

local i

for i in $* ; do

echo "i is $i"

done

}

$*是字符串:以"参数1 参数2 ... " 形式保存所有参数

批量改文件名

for i in *.txt *.txt相当于一个字符串数组,依次循环赋值给i

do

mv "$i" "$i.bak"

done

利用for in克服` `和$( ) 的多行合为一行的缺陷

for i in $(ls *.txt)

do

echo $i

done

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: