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
==============================================
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
相关文章推荐
- Bash shell script 取到文件中每一行(Just for record)
- bash学习之一:登陆、非登陆shell,交互、非交互shell,以及它们的startup文件
- Linux Bash Shell学习(五):特殊文件、别名、选项和参数
- shell学习之系统文件备份之脚本分析
- Bash Shell 学习记录(一)
- Linux 学习笔记 -- 第三部分 学习 shell 与 shell script -- 第12章 正则表达式与文件格式化处理
- Bash&nbsp;Shell&nbsp;快捷键的学习使用
- cocos2d-x 3.x游戏开发学习笔记(2)--cocos2d-x 3.x文件目录结构分析
- 【鸟哥的linux私房菜-学习笔记】Bash shell之管线命令
- shell的初始化文件:/etc/profile和/etc/bashrc(bash.bashrc)
- shell或bash中的 判断文件是否存在的脚本语法
- hadoop相关启动脚本分析 有助于troubleshoot,顺便学习shell
- shell学习之read:从键盘或文件中获取标准输入
- hadoop 文件上传源码分析-------学习
- 【鸟哥的linux私房菜-学习笔记】认识与分析登录文件
- shell的初始化文件:/etc/profile和/etc/bashrc(bash.bashrc)
- shell学习四十二天----使用touch更新文件时间
- bash 按照文件日期创建文件(学习备份)
- shell学习四十六天----文件系统的空间信息df和du命令
- bash shell 合并文件