您的位置:首页 > 数据库 > Redis

golang操作Redis(redigo基础篇)

2014-08-21 23:42 253 查看
SHELL

chsh -l 查看shell

切换

1.临时 直接执行 /bin/sh

2.永久 chsh

bash 特点、

1.交互式

2.命令的历史功能 history !+指令编号

3.命令的补齐 TAB

4.通配符 *任意字符 ?匹配一个字符 【!1-9】

5.前台后台切换 fg bg

ps -aux 查看进程

jobs 查看正在进行或暂停任务

bg 1 将一个任务切换到后台运行

fg 1 将任务切换到前台运行

6.特殊字符 若引用 “” 强引用 '' 命令取代 ``

7.输入输出重定向

0 标准输入

1 标准输出

2 变准出错

&> 所有的输出 = 2>&1

find / -size +10M 2>1.txt 将出错的输入到1.txt中

mail root -s ok1 0< 1.txt 正确的输入到1.txt并以邮件形式发送出去

8.命令别名

环境变量

set

1.本地变量

2.环境变量

3. 特殊变量

a 位置变量 $1 $2 $0显示shell名

b 状态变量

环境文件

系统 /etc/profile /etc/bashrc

个人 ~/.bash_profile ~/bashrc

umask 权限掩码

022

最高权限-umask=实际权限

文件文件最高权限 666

目录最高权限 777

umask 022 修改权限为022

vim /etc/bashrc 编辑权限设置

alias=`find / -size +10m` 执行alias这自动收索大于10M的文件

alias find10=`find / -size +10M` 执行find10这自动收索大于10M的文件

算数运算

let

A=let 1+2 对1+2进行求和

$【】

A=$[1+2] 对1+2进行求和

$(())

A=$((1+3)) 对1+3进行求和

bc

echo "scale=2;5/2" |bc 求5/2的值

判断

test expression

test 1 -gt 2

echo $? 判断1是否大于2

【 表达式 】

【 1 -gt 2】

echo $? 判断1是否大于2



数字比较


大于 -gt

大于等于 -ge

小于 -lt

小于等于 -le

等于 -eq

不等于 ! -eq

字符串比较

大于 > (一对【】带转义符 两队【】 不加转义符)

A=hello ,b=hellol

[ $A \> $B ] [[ $A > $B ]] 判断A是否大于B

小于 < (一对【】带转义符 两队【】 不加转义符)

等于 = == (等号两边有空格)

[ $A = $B ]

对象

-f 文件

[ -f f1 ]

echo $? 判断f1是否为一个文件

-e 存在

-d 目录

-L 连接

-r 读取

-x 执行

与 -a (两个判断式) 或 -o

[ -e f1 -a -r f1 ] 判断f1是存在与可读取

短路操作符

&& 与(两个语句) ||

grep "^user1\>" /etc/passwd && echo "the account is exist " ||echo "the account is not exist"

在/etc/passwd下收索user1,如果存在显示the account is exist否则显示the account is not exist

grep "^user1\>" /etc/passwd &>/dev/null && echo "the account is exist " ||echo "the account is not exist"

在/etc/passwd下收索user1,如果存在显示the account is exist否则显示the account is not exist,并将收索结果放置/dev/null中

用脚本实现

vim 1.sh

#!/bin/bash

read -p "please input one account:" ACCOUNT

grep "^$ACCOUNT\>" /etc/passwd &>/dev/null && echo "the $ACCOUNT is ok " ||echo "not ok "

echo -e "\t\033[31m123\033[0m\n" 将123用红色表示



控制语句


选择

1。单选

if 【】;then

.....


fi

#!/bin/bash

read -p "please input one account:" ACCOUNT

if grep "^$ACCOUNT\>" /etc/passwd &>/dev/null;then

echo "the $ACCOUNT is ok "

fi

2.双选

if [];then

..


else

..

fi


#!/bin/bash

read -p "please input one account:" ACCOUNT

if grep "^$ACCOUNT\>" /etc/passwd &>/dev/null;then

echo "the $ACCOUNT is ok "

else

echo “the $ACCOUNT is not ok”

fi

3.多选

if 【】;then

......


elif 【】;then

......


elif 【】;then

......


fi

判断一个文件是否存在,并判断它的类型

#!/bin/bash

read -p "please input one file:" FILE

read -p "please input one dir:" DIR

if [ -e $DIR/$FILE ];then

if [ -f $DIR/$FILE ];then

echo "the $DIR/$FILE is a file"

elif [ -d $DIR/$FILE ];then

echo "the $DIR/$FILE is a dir"

elif [ -L $DIR/$FILE ];then

echo "the $DIR/$FILE is a link"

fi

else

echo "the $DIR/$FILE is not ok"

fi

3.case语句

case 变量 in

变量值1)

.......;;


变量值2)

......;;

变量值3)



....;;


*)

....;;

esac

shell 脚本实现判断文件类型

#!/bin/bash

read -p "please input one file:" FILE

read -p "please input one dir:" DIR

if [ -e $DIR/$FILE ];then

STRING=`/bin/ls -l -d $DIR/$FILE`

FIRSTCHAR=`echo $(STRING:0:1)`

case $FIRSTCHAR in

-)

echo "the $DIR/$FILE is file";;

d)

echo "the $DIR/$FILE is dir";;

l)

echo "the $DIR/$FILE is link";;

b)

echo "the $DIR/$FILE is block device";;

c)

echo "the $DIR/$FILE is char device";;

*)

exit

else

echo "the $DIR?$FILE is not exist"

exit

esac

fi

脚本运行的时候 ,提示输入帐号,判断该帐号是否存在 ,如果存在 显示该用户的shell 以及改用的家目录,以及该用户密码是否设置

#!/bin/bash

read -p "please input one account:" ACCOUNT

if grep "^$ACCOUNT\>" /etc/passwd &>/dev/null;then

HOMEDIR1=`grep "^$ACCOUNT\>" /etc/passwd |cut -d: -f6`

shell1=`grep "^$ACCOUNT\>" /etc/passwd |cut -d: -f7`

echo "the user $ACCOUNT is exist"

echo "the user home is $HOMEDIR1"

echo "the user shell is $shell1"

/usr/bin/passwd -S $ACCOUNT &>/tmp/state

if grep -i "lock" /tmp/state &>/dev/null;then

echo "passwd is lock"

elif grep -i "empty" /tmp/state &>/dev/null;then

echo "passwd is empty"

elif grep -i "md5" /tmp/state &>/dev/null;then

echo "passwd is encry"

fi

else

echo "the user $ACCOUNT is not exist"

fi



循环


for 变量 in 变量值;do

........

done


1到100求和

#!/bin/bash

I=1

SUM=0

for I in {1..100};do

let SUM=$SUM+$I

done

echo $SUM

1到100内偶数求和

#!/bin/bash

I=1

SUM=0

for I in {1..100};do

if [$[ $I%2 ] = 0 ];then

let SUM=$SUM+$I

done

echo $SUM

1到100内奇数求和

#!/bin/bash

I=1

SUM=0

for I in {1..100};do

if [$[ $I%2 ] != 0 ];then

let SUM=$SUM+$I

done

echo $SUM

while [条件];do

done

#!/bin/bash

I=1

SUM=0

while [ $I -le 100 ];do

let SUM=$SUM+$I

let I=$I+1

done

echo $SUM

while read LINE;do

done <文件名

查看一个文件内容在每个目录前显示行号

#!/bin/bash

let I=1

while read LINE;do

echo -e "$I:\t" $LINE

let I=$I+1

done </etc/passwd

显一个文件内容并判断文件类型

#!/bin/bash

read -p "please input one directory:" DIR

/bin/ls $DIR &>/tmp/file

while read LINE;do

if [ -f $dir/$LINE ];then

echo -e "$LINE\t" "the $DIR/$LINE is a file"

elif [ -d $DIR/$LINE ];then

echo -e "$LINE\t" "the $DIR/$LINE is a dir"

elif [ -L $DIR/$LINE ];then

echo -e "$LINE\t" "the $DIR/$LINE is a link"

done </tmp/file

until [条件];do

done

#!/bin/bash

#!/bin/bash

I=1

SUM=0

until [ $I -ge 100 ];do

let SUM=$SUM+$I

let I=$I+1

done

echo $SUM

作业 挑选 普通帐号 ,没账号的前面加上序号

#!/bin/bash

I=1

while read LINE;do

USERID=`echo $LINE |cut -d: -f3`

ACCOUNTNAME=`echo $LINE |cut -d: -f1`

if [ $USERID -ge 500 ];then

echo -e "\033[31m$I\t$ACCOUNTNAME\t$USERID\033[0m"

let I=$I+1

fi

done </etc/passwd

判断一个网络某个用户是否存在

#!/bin/bash

for I in {1..254};do

if ping -c 1 -W 1 192.168.101.$I &>/dev/null;then

echo "the host 192.168.101.$I is exist"

else

echo "the host 192.168.101.$I is not exist"

fi

done

cut /etc/passwd -d: -f3 |sort -n |tail -2 |head -1 查看当前用户最大uid

不用useradd创建一个用户并以脚本的方式实现

#!/bin/bash

read -p "please input one user:" ACCOUNT

if grep "^$ACCOUNT\>" /etc/passwd &>/dev/null;then

exit

else

MAXID=`cut -d:-f3 /etc/passwd |sort -n |tail -2 |head -1`

echo "$ACCOUNT:x:$[ $MAXID+1 ]:$[ $MAXID+1]::/home/$ACCOUNT:/bin/bash" >>/etc/passwd

echo "$ACCOUNT::::::::">>/etc/shadow

echo “$ACCOUNT:x:$[ $MAXID+1 ]:" >>/etc/group

mkdir /home/$ACCOUNT

cp /etc/skel/.* /home/$ACCOUNT &>/dev/null

chown -R $ACCOUNT.$ACCOUNT /home/$ACCOUNT

touch /var/spool/mail/$ACCOUNT

chmod 660 /var/spool/mail/$ACCOUNT

chown $ACCOUNT.mail /var/spool/mail/$ACCOUNT

echo "123" |passwd --stdin $ACCOUNT

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