2016年11月

使用 `parted` 命令分区

在一些fdisk不支持的硬盘上,可以使用这个命令我的 minimal的系统没有这个命令,需要单独安装
使用help可以查看帮助,就不解释含义了,不支持某些格式文件系统(例如ext3和ext4)的格式化,可以在这里分区,再退出用其他的命令格式化分区

[root@jin ~]# parted /dev/sdb
GNU Parted 2.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
# 分区,执行即生效
(parted) mkpart primary ext4 16.5MB 32.5MB
警告: WARNING: the kernel failed to re-read the partition table on /dev/sdb (设备或资源忙).  As a result, it may not reflect all
of your changes until after reboot.
# 查看分区
(parted) p                                                                
Model: Msft Virtual Disk (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos

Number  Start   End     Size    Type     File system  标志
 1      32.3kB  16.4MB  16.4MB  primary  ext4
 2      16.8MB  32.5MB  15.7MB  primary
创建设备文件

分区之后,如果没有创建设备文件,则需要手动创建

# 查看创建设备文件需要的参数
[root@jin ~]# ls -al /dev/sdb*
brw-rw---- 1 root disk 8, 16 11月 18 09:28 /dev/sdb
brw-rw---- 1 root disk 8, 17 11月 17 15:56 /dev/sdb1
brw-rw---- 1 root disk 8, 18 11月 17 16:14 /dev/sdb2
# 创建设备文件,参数分别为: 设备文件名, 设备类型(对应上面查看的文件的类型,第一个字母就代表文件类型),装置代码(要和上面的一致),(次要装置代码,和上面的要不一样),参考下面是如何设置的
[root@jin ~]# mknod /dev/sdb3 b 8 19

上面的执行完之后确实创建了一个设备文件,修改文件权限和文件所数用户和所属数组之后,不能对文件进行格式化

查看

[root@jin ~]# fdisk -l /dev/sdb

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x0008d235

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1           2       16032+  83  Linux
Partition 1 does not end on cylinder boundary.
Partition 1 does not start on physical sector boundary.
/dev/sdb2               3           4       15360   83  Linux
Partition 2 does not end on cylinder boundary.
/dev/sdb3               4           6       15360   83  Linux
Partition 3 does not end on cylinder boundary.

获得使用start_udev命令自动创建,(我执行这个命令无效)

格式化

上面的步骤执行完毕失败的话,再运行了partprobe,如果还是无效,就重启在格式化吧,格式化就用正常的格式化命令就行

mkfs.ext4 /dev/sdb2 

添加新硬盘

原本的一块硬盘是sda,新加了一块硬盘为sdb

查看新硬盘
[root@jin dev]# fdisk -l

Disk /dev/sda: 136.4 GB, 136365211648 bytes
255 heads, 63 sectors/track, 16578 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x000d203d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        6400    51200000   83  Linux
/dev/sda3            6400       12774    51200000   83  Linux
/dev/sda4           12774       16579    30563328    5  Extended
/dev/sda5           12774       13029     2048000   82  Linux swap / Solaris
/dev/sda6           13030       16579    28513280   83  Linux

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x00000000
分区
[root@jin dev]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xf0f50ac9.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

The device presents a logical sector size that is smaller than
the physical sector size. Aligning to a physical sector (or optimal
I/O) size boundary is recommended, or performance may be impacted.

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
# n 新建分区
Command (m for help): n
# 选择分区类型
Command action
   e   extended
   p   primary partition (1-4)
p
# 分区数字编号
Partition number (1-4): 1
# 开始柱面
First cylinder (1-130, default 1):  
Using default value 1
# 结束柱面,直接写"+存储容量"也行
Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): +10M
# 打印当前磁盘分区就能看到了
Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x752ab7eb

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1           2       16032+  83  Linux
# 下面这个不用管
Partition 1 does not end on cylinder boundary.
Partition 1 does not start on physical sector boundary.
# w写入,如果没有这一步的话,刚刚的操作无效
Command (m for help): w    
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
格式化分区

如果系统版本较老(centOS 5或之前的版本需先运行partprobe命令)

[root@jin dev]# mkfs.ext4 /dev/sdb1 
mke2fs 1.41.12 (17-May-2010)
/dev/sdb1 alignment is offset by 512 bytes.
This may result in very poor performance, (re)-partitioning suggested.
Discarding device blocks: 完成                            
文件系统标签=
操作系统:Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=4 blocks, Stripe width=0 blocks
4016 inodes, 16032 blocks
801 blocks (5.00%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=16515072
2 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks: 
    8193

正在写入inode表: 完成                            
Creating journal (1024 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 22 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
挂载
[root@jin ~]# mount /dev/sdb1 testDir
配置启动加载
查看磁盘 UUID

dumpe2fs 1.41.12 (17-May-2010) Filesystem volume name: <none> Last mounted on: <not available> Filesystem UUID: 5634cbae-e0fa-4d12-9ae2-031c5cbda12d ...
编辑vim /etc/fstab文件
UUID=7cfd829d-d09b-4efd-bc9b-5ac19100c25f /                       ext4    defaults        1 1 
UUID=39eac6d2-0188-4a1f-a795-5564bc7c2187 /boot                   ext4    defaults        1 2 
UUID=e5f913f4-97d2-4a0d-92a8-e95bb59be591 /home                   ext4    defaults        1 2 
UUID=b37cd8cf-392a-4587-83ad-6ea0be1c4a11 /var                    ext4    defaults        1 2 
UUID=e3cdd9d3-3281-416d-bfed-0f3c0c3671a0 swap                    swap    defaults        0 0 
# 新添加的一行,分别是,设备UUID,所要挂载到文件,挂载选项(man mount搜索defaults可以查看),dump备份时的时间周期设置,开机检测文件系统的顺序
UUID=5634cbae-e0fa-4d12-9ae2-031c5cbda12d /root/testDir           ext4    defaults        0 1 
tmpfs                   /dev/shm                tmpfs   defaults        0 0 
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0 
sysfs                   /sys                    sysfs   defaults        0 0 
proc                    /proc                   proc    defaults        0 0

nginx内置预定义变量

nginx的配置文件中可以使用的内置变量以美元符$开始,也有人叫全局变量。其中,部分预定义的变量的值是可以改变的。
$arg_PARAMETER 这个变量值为:GET请求中变量名PARAMETER参数的值。

$args 这个变量等于GET请求中的参数。例如,foo=123&bar=blahblah;这个变量只可以被修改

$binary_remote_addr 二进制码形式的客户端地址。

$body_bytes_sent 传送页面的字节数

$content_length 请求头中的Content-length字段。

$content_type 请求头中的Content-Type字段。

$cookie_COOKIE cookie COOKIE的值。

$document_root 当前请求在root指令中指定的值。

$document_uri 与$uri相同。

$host 请求中的主机头(Host)字段,如果请求中的主机头不可用或者空,则为处理请求的server名称(处理请求的server的server_name指令的值)。值为小写,不包含端口。

$hostname 机器名使用 gethostname系统调用的值

$http_HEADER HTTP请求头中的内容,HEADER为HTTP请求中的内容转为小写,-变为_(破折号变为下划线),例如:$http_user_agent(Uaer-Agent的值), $http_referer...;

$sent_http_HEADER HTTP响应头中的内容,HEADER为HTTP响应中的内容转为小写,-变为_(破折号变为下划线),例如: $sent_http_cache_control, $sent_http_content_type...;

$is_args 如果$args设置,值为"?",否则为""。

$limit_rate 这个变量可以限制连接速率。

$nginx_version 当前运行的nginx版本号。

$query_string 与$args相同。

$remote_addr 客户端的IP地址。

$remote_port 客户端的端口。

$remote_user 已经经过Auth Basic Module验证的用户名。

$request_filename 当前连接请求的文件路径,由root或alias指令与URI请求生成。

$request_body 这个变量(0.7.58+)包含请求的主要信息。在使用proxy_pass或fastcgi_pass指令的location中比较有意义。

$request_body_file 客户端请求主体信息的临时文件名。

$request_completion 如果请求成功,设为"OK";如果请求未完成或者不是一系列请求中最后一部分则设为空。

$request_method 这个变量是客户端请求的动作,通常为GET或POST。
包括0.8.20及之前的版本中,这个变量总为main request中的动作,如果当前请求是一个子请求,并不使用这个当前请求的动作。

$request_uri 这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI。

$scheme 所用的协议,比如http或者是https,比如rewrite ^(.+)$ $scheme://example.com$1 redirect;

$server_addr 服务器地址,在完成一次系统调用后可以确定这个值,如果要绕开系统调用,则必须在listen中指定地址并且使用bind参数。

$server_name 服务器名称。

$server_port 请求到达服务器的端口号。

$server_protocol 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。

$uri 请求中的当前URI(不带请求参数,参数位于$args),不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改。不包括协议和主机名,例如/foo/bar.html

转自 http://www.nginx.cn/273.html

shell脚本中的数值运算

整数类型的计算

echo $[1+1];
2

小数
再用上面的方式就不行了

echo $[1+1.1];
-bash: 1+1.1: syntax error: invalid arithmetic operator (error token is ".1")

可以bs命令,但是我最小化安装的系统没有这个命令,需要安装下

echo "scale=2;1+1.1" | bc;
2.1

还可以用awk 命令,但是这个命令对复杂点的运算就太麻烦了

echo "1+1.1" | awk -F "+" '{print $1+$2}'

复杂点的计算

#!/bin/bash
var1=1.1;
var2=2.2;
var1=3.3;
var4=`bc << EOF
scale=3
intervar=6.66
res=(${var1}+${var2}*${var3}-intervar/${var1})
res
EOF
`
echo $var4;

执行结果

bash test.sh 
-1.140

参考 http://blog.csdn.net/seteor/article/details/17377963

Linux shell编程——if条件判断

if语句格式
if command;then
    command;
else
    command;
fi

示例

#!/bin/bash
if grep "root" /etc/passwd;then
        echo "true";
else
        echo "false";
fi;
if 的三种条件表达式
格式 说明
if command;then
if function;then
命令执行成功,等于返回0 (比grep ,找到匹配)
执行失败,返回非0 (grep,没找到匹配)
if [ expression_r_r_r ];then 表达式结果为真,则返回0,if把0值引向then
if test expression_r_r_r ;then 表达式结果为真,则返回0,if把0值引向then
#!/bin/bash
# 输出false
if test 1 < 2 ;then
        echo "true";
else
        echo "false";
fi;
# 输出false
if [ 1 < 2 ];then
        echo "true";
else
        echo "false";
fi;
[ ] && ——快捷if
格式 说明
[ expression_r_r_r ] && command && 可以理解为then,如果左边的表达式为真则执行右边的语句
[ -f /etc/passwd ] && echo "exists";
exists
shell的if与c语言if的功能上的区别
shell if c语言if
0为真,走then 正好相反,非0走then
不支持整数变量直接if
必须:if [ i –ne 0 ]
但支持字符串变量直接if
if [ str ] 如果字符串非0
支持变量直接if
if (i )
#!/bin/bash
# 输出true
if [ 0 ];then
        echo "true";
else
        echo "false";
fi;
# 输出true
if [ 1 ];then
        echo "true";
else
        echo "false";
fi;


以command作为if 条件
以多条 command 作为条件
#!/bin/bash
echo -n "input:";
read user;
# 多条指令,这些命令之间相当于“and”(与)
if grep ${user} /etc/passwd > /tmp/null;who -u | grep $user;then
        echo "${user} has logged";
else
        echo "${user} has not logged";
fi;

执行结果

[root@jin test]# bash ../test.sh 
input:root
root     pts/1        2016-11-12 09:22   .          5577 (192.168.1.62)
root has logged
[root@jin test]# bash ../test.sh 
input:jin
jin has not logged
以函数作为if条件 (函数就相当于command,函数的优点是其return值可以自定义)
#!/bin/bash
function yesorno()
{
        return 1;
}
# 以函数作为if条件
if yesorno;then
        echo "return is 0,true";
else
        echo "return is not 0,false";
fi;

执行结果

bash ../test.sh 
return is not 0,false
if command 等价于 command+if $?
#!/bin/bash
echo -n "input:";
read user;
# 多条指令,这些命令之间相当于“and”(与)
grep ${user} /etc/passwd > /tmp/null;who -u | grep $user;
# $? 其实是上次命令的返回值
if [ $? -eq 0 ];then
        echo "${user} has logged";
else
        echo "${user} has not logged";
fi;
以条件表达式作为 if条件

传统if 从句子——以条件表达式作为 if条件

if [ 条件表达式 ]
then
 command
 command
 command
else
 command
 command
fi
条件表达式
  • 文件表达式
    |表达式|说明|
    |:-:|:-|
    |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 ] 如果a等于b 字符串允许使用赋值号做等号
    if [ $string1 != $string2 ] 如果string1不等于string2
    if [ -n $string ] 如果string 非空(非0),返回0(true)
    if [ -z $string ] 如果string 为空
    if [ $sting ] 如果string 非空,返回0 (和-n类似)
条件表达式引用变量要带$
a=a;
b=a;
# 输出 false,因为字母a不等于字母b
if [ a = b ];then
        echo "true";
else
        echo "false";
fi;
a=a;
b=a;
# 输出 true,因为变量a等于变量b
if [ $a = $b ];then
        echo "true";
else
        echo "false";
fi;

-eq -ne -lt -nt只能用于整数,不适用于字符串,字符串等于用赋值号=

#!/bin/bash
if [ "aa" -eq "aa" ];then
        echo "true";
else
        echo "false";
fi;

执行结果

../test.sh: line 2: [: aa: integer expression expected
false

=放在别的地方是赋值,放在if [ ] 里就是字符串等于,shell里面没有==的,那是c语言的等于
无空格的字符串,可以加" ",也可以不加

#!/bin/bash
a="aaa";
b=aaa;
# 输出结果为true
if [ $a = $b ];then
        echo "true";
else
        echo "false";
fi;

= 作为等于时,其两边都必须加空格,否则失效
等号也是操作符,必须和其他变量,关键字,用空格格开 (等号做赋值号时正好相反,两边不能有空格)

#!/bin/bash
a="aaaa";
b="aaa";
# 输出true,因为if把$a=$b连读成一个变量,这个表达式实际是字串 "aaaa=aaa",所以走 then
if [ $a=$b ];then
        echo "true";
else
        echo "false";
fi

If [ $ANS ] 与 if [ -n $ANS ]
-n会把空格,回车也识别成非空

#!/bin/bash
echo "enter something,please\n";
read str;
#if [ $str ];then
if [ -n $str ];then
        echo $str;
else
        echo "empty";
fi;

整数条件表达式,大于,小于,shell里没有> 和< ,会被当作尖括号,只有-ge,-gt,-le,lt

#!/bin/bash
echo "enter a number,please\n";
read str;
if [ $str -lt 100 ];then
        echo "$str < 100\n";
else
        echo "$str >= 100\n";
fi;

逻辑表达式

表达式 例子 说明
if [ ! 表达式 ] if [ ! -d $num ] 如果不存在目录$num
if [ 表达式1 –a 表达式2 ] if [ $str -lt 100 -a $str -gt 10 ] 如果$str小于100并且大于10
if [ 表达式1 –o 表达式2 ] if [ $str -lt 100 -a $str -gt 10 ] 如果$str小于100或者大于10
  • 表达式与前面的= != -d –f –x -ne -eq -lt等合用
  • 逻辑符号就正常的接其他表达式,没有任何括号( ),就是并列if [ -z "$JHHOME" -a -d $HOME/$num ]
  • 注意逻辑与-a与逻辑或-o很容易和其他字符串或文件的运算符号搞混了

以 test 条件表达式 作为if条件
if test $num -eq 0 等价于 if [ $num –eq 0 ]

#!/bin/bash
echo "enter a number,please\n";
read str;
# 下面两种if,效果相同
if test $str -lt 100 -a $str -gt 10 ;then
#if [ $str -lt 100 -a $str -gt 10 ];then
        echo "$str < 100\n";
else
        echo "$str >= 100\n";
fi;

if简化语句
&& 如果是“前面”,则“后面”

# 变量不为空则输出变量
[ -n $HOME ] && echo $HOME;

|| 如果不是“前面”,则后面

# 如果变量为空,则输出提示
[ -n $HOME ] || echo '$HOME is empty'

用简化 if 和$1,$2,$3来检测参数,不合理就调用help
[ -z "$1" ] && help 如果第一个参数不存在(-z 字符串长度为0 )
[ "$1" = "-h" ] && help 如果第一个参数是-h,就显示help

转载自 http://blog.csdn.net/buutterfly/article/details/6615162