频道栏目
首页 > 资讯 > Sybase > 正文

shell基础3

15-06-23        来源:[db:作者]  
收藏   我要投稿

cut是一个选取命令:

就是将一段数据经过分析,取出我们想要的。一般来说,选取信息通常是针对“行”来进行分析的,并不是整篇信息分析的。

(1)其语法格式为:

cut  [-bn] [file] 或 cut [-c] [file]  或  cut [-df] [file]

使用说明
cut 命令从文件的每一行剪切字节、字符和字段并将这些字节、字符和字段写至标准输出。
如果不指定 File 参数,cut 命令将读取标准输入。必须指定 -b、-c 或 -f 标志之一。

主要参数
-b :以字节为单位进行分割。这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志。
-c :以字符为单位进行分割。
-d :自定义分隔符,默认为制表符。
-f :与-d一起使用,指定显示哪个区域。
-n :取消分割多字节字符。仅和 -b 标志一起使用。如果字符的最后一个字节落在由 -b 标志的 List 参数指示的范围之内,该字符将被写出;否则,该字符将被排除。


cut的使用

Using the cut command with /etc/passwd
For a first cut command example, I’ll use the /etc/passwd file on my Unix system. I use this file because fields in the file are separated by the “:” character, which make it very easy to work with.

Using that file, here’s a Linux cut command that prints the first field (first column) of every line in that file:

cut -f1 -d: /etc/passwd

This cut command can be read as:

Print the first field (-f1)
Fields are delimited by the “:” character (-d:)
Use the /etc/passwd file as input
The output of this command will vary by Unix and Linux systems, but it will be the first field of your /etc/passwd file, which contains the name of the users on your system. This will look something like:

nobody
alvin
george
fred

#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

for value in dog "cat" 'bird'   #注意 加引号没发现有什么特别的
do
        echo "animal are ${value}s"
done

fuhui@ubuntu:~/script$ sh sh22.sh 
animal are dogs
animal are cats
animal are birds

linux 给用户加sudo权限
1、用root帐号登录或者su到root。

2、增加sudoers文件的写权限: chmod u+w /etc/sudoers

3、vim /etc/sudoers 找到 root ALL=(ALL) ALL 在这行下边添加 dituhui ALL=(ALL) ALL (ps:dituhui代表是你要添加sudo权限的用户名)

4、除去sudoers文件的写权限: chmod u-w /etc/sudoers


passwd下我的账户信息

fuhui:x:1000:1000:FirstLinux,,,:/home/fuhui:/bin/bash

命令:id 功能说明:查看显示目前登陆账户的uid和gid及所属分组及用户名

fuhui@ubuntu:~/script$ id fuhui
uid=1000(fuhui) gid=1000(fuhui) groups=1000(fuhui),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),108(lpadmin),124(sambashare)

命令:finger

功能说明:查询用户的信息,通常会显示系统中某个用户的用户名、主目录、停滞时间、登录时间、登录shell等信息。如果要查询远程机上的用户信息,需要在用户名后面接“@主机名”,采用[用户名@主机名]的格式,不过要查询的网络主机需要运行finger守护进程。

Login: fuhui                            Name: FirstLinux
Directory: /home/fuhui                  Shell: /bin/bash
On since Sun Jun 21 18:22 (PDT) on pts/12 from 192.168.187.1
   7 seconds idle
No mail.
No Plan.

#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

users=$(cut -f1 -d: /etc/passwd)

for name in $users
do
        id $name   #注意,直接执行命令语句
        finger $name
done

linux seq更为详细的用法

seq命令的作用就是打印出一串有序的数字,seq(sequence of number).

它主要有以下3个参数构成:

       -f, --format=FORMAT 
          use printf style floating-point FORMAT (default: %g)

-f 指定打印的格式:
例如:

[root@hao32]# seq -f %05g 2 7 
00002
00003
00004
00005
00006
00007
 -s, --separator=STRING
          use STRING to separate numbers (default: \n)

-s 指定分隔符 默认是回车(-s后面可以不带引号):
例如:

[root@hao32]# seq -s" " 2 7     
2 3 4 5 6 7
   -w, --equal-width
          equalize width by padding with leading zeroes

-w 输出是同宽 前面不足的用 “0” 补全,即与位数最多的数对齐
例如:

[root@hao32]# seq -w 2 11
02
03
04
05
06
07
08
09
10
11

/dev/null 2>&1 的作用

首先需要了解unix系统的输入输出流,他们分别与数字的对应关系是:

    0:标准输入流( stdin )
    1 : 标准输出流( stdout )
    2 : 标准错误流( stderr )

所以 2>&1 表示的意思是将 stderr 重定向到 stdout, 并一起在屏幕上显示。如果不加数字,默认的重定向是针对 stdout 的。

语句:/dev/null 代表空设备文件; >代表重定向; 2代表标准错误流; &表示引用,等同的意思。所以语句的意思是,将标准输出以及标准错误输出重定向到空设备文件,将信息屏蔽不显示。


#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

network='192.168.1'

for sitenu in $(seq 1 10)
do      #注意${sitenu}和$${sitenu}是完全不相同的
        ping -c 1 -w 1 "${network}.${sitenu}" > /dev/null && result=0 || result=1  #注意 > /dev/null,没有找到-w的用处
        if [ "$result" = 0 ]; then
                echo  "${network}.${sitenu} is up"
        else
                echo "${network}.${sitenu} is down"
        fi
done

使用变量的值和变量赋值的区别是,是否存在前缀$

ls -lh $dir > /dev/null 如果是这样写的话,完了,没有输出
! -d $dir 判断目录是否存在,注意写法
#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

read -p "please input a directory:" dir

if [ $dir = '' -o ! -d $dir ]; then
        echo "the $dir is not existed in the system"
        exit 1
fi

for info in $( ls $dir)
do
        perm=''
        test -r "$dir/$info" && perm="$perm readable"
        test -w "$dir/$info" && perm="$perm writeable"
        test -x "$dir/$info" && perm="$perm executable"
        echo "the $dir/$info permission $perm"
done

for循环的另一种输入方式

for (( 初始值; 限定值; 步长 ))
do
    程序段
done
#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

read -p "please input a number:" nu
declare -i sum=0

for((i=1; i<=$nu; i=i+1))	#注意,这里并没有使用$i
do
        sum=$(($sum+$i))
done

echo "the total is : $sum"




这里测试加上$
#!/bin/bash
# program:test example 01
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH

read -p "please input a number:" nu
declare -i sum=0

for((i=1; $i<=$nu; i=$i+1))        #修改成了一般的认识,发现仍然可以,感觉这种更符合平常逻辑
do
        sum=$(($sum+$i))
done

echo "the total is : $sum"




下面的错误应该不是$i的写法的,不明白哪里语法错误了
fuhui@ubuntu:~/script$ sh -n sh27.sh 
sh27.sh: 9: sh27.sh: Syntax error: Bad for loop variable

判断脚本是否存在语法错误

[root@www ~]# sh [-nvx] scripts.sh  

选项参数:

-n :不执行script,仅查询语法是否存在问题;
-v :再执行 sccript 前,先将 scripts 的内容输出到屏幕上;
-x :将使用到的 script 内容显示到屏幕上,这是很有用的参数!

fuhui@ubuntu:~/script$ sh -x sh27.sh 
+ PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
+ export PATH
+ read -p please input a number: nu
please input a number:
相关TAG标签
上一篇:如何查看一个进程打开哪些fd及对应的文件或套接字操作
下一篇:normal数据库关闭hang的问题
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站