rokevin
移动
前端
语言
  • 基础

    • Linux
    • 实施
    • 版本构建
  • 应用

    • WEB服务器
    • 数据库
  • 资讯

    • 工具
    • 部署
开放平台
产品设计
  • 人工智能
  • 云计算
计算机
其它
GitHub
移动
前端
语言
  • 基础

    • Linux
    • 实施
    • 版本构建
  • 应用

    • WEB服务器
    • 数据库
  • 资讯

    • 工具
    • 部署
开放平台
产品设计
  • 人工智能
  • 云计算
计算机
其它
GitHub
  • shell

shell

shell脚本语言(超全超详细) https://blog.csdn.net/weixin_43288201/article/details/105643692

https://blog.csdn.net/weixin_42031299/article/details/120107370

https://blog.csdn.net/will_csdn_go/article/details/117629033

Mac环境配置adb不起作用解决方案

在mac下面配置adb之后一直都不起作用,每次使用都需要source .bash_profile后才能使用. 今天大神提醒,是zsh的问题。

解决方案: 在.bash_profile中添加(各种教程中都有)

export PATH=${PATH}:/Users/james/Documents/adt-bundle-mac/sdk/platform-tools/;Applications/Android Studio.app/sdk/platform-tools 然后找到User目录下面的.zshrc文件,我的在james/.zshrc.在文件最后添加

Enable my profile

source ~/.bash_profile

http://blog.csdn.net/mobilexu/article/details/41549277

curl请求处理返回结果

result=`curl -d "driverAccount=xxxx&password=xxxx" http://test.xxxx.cn/driver/user/login -s`

echo $result

function getJsonValuesByAwk() {
    awk -v json="$1" -v key="$2" -v defaultValue="$3" 'BEGIN{
        foundKeyCount = 0
        while (length(json) > 0) {
            # pos = index(json, "\""key"\""); ## 这行更快一些,但是如果有value是字符串,且刚好与要查找的key相同,会被误认为是key而导致值获取错误
            pos = match(json, "\""key"\"[ \\t]*?:[ \\t]*");
            if (pos == 0) {if (foundKeyCount == 0) {print defaultValue;} exit 0;}

            ++foundKeyCount;
            start = 0; stop = 0; layer = 0;
            for (i = pos + length(key) + 1; i <= length(json); ++i) {
                lastChar = substr(json, i - 1, 1)
                currChar = substr(json, i, 1)

                if (start <= 0) {
                    if (lastChar == ":") {
                        start = currChar == " " ? i + 1: i;
                        if (currChar == "{" || currChar == "[") {
                            layer = 1;
                        }
                    }
                } else {
                    if (currChar == "{" || currChar == "[") {
                        ++layer;
                    }
                    if (currChar == "}" || currChar == "]") {
                        --layer;
                    }
                    if ((currChar == "," || currChar == "}" || currChar == "]") && layer <= 0) {
                        stop = currChar == "," ? i : i + 1 + layer;
                        break;
                    }
                }
            }

            if (start <= 0 || stop <= 0 || start > length(json) || stop > length(json) || start >= stop) {
                if (foundKeyCount == 0) {print defaultValue;} exit 0;
            } else {
                print substr(json, start, stop - start);
            }

            json = substr(json, stop + 1, length(json) - stop)
        }
    }'
}
echo "\r"

# 通过key获取json字符串里的value值 获取不到返回默认值
#stateMessage=`getJsonValuesByAwk "json字符串" "json中的key" "默认值"`

stateMessage=`getJsonValuesByAwk "$result" "stateMessage" "-1"`

#stateMessage='"成功"'

# 去掉获取结果里的 " 引号
stateMessage=`echo $stateMessage | sed 's/"//g'`

echo ${stateMessage}"哈哈"

学习资源

http://www.92csz.com/study/linux/12.htm

shell运算符

https://www.cnblogs.com/lingling99/p/3584895.html

shell启动脚本中的0、1、2、>和&解析

当初在shell中, 看到">&1"和">&2"始终不明白什么意思.经过在网上的搜索得以解惑.其实这是两种输出.

在 shell 程式中,最常使用的 FD (file descriptor) 大概有三个, 分别是:

0 是一个文件描述符,表示标准输入(stdin) 1 是一个文件描述符,表示标准输出(stdout) 2 是一个文件描述符,表示标准错误(stderr)

在标准情况下, 这些FD分别跟如下设备关联: stdin(0): keyboard 键盘输入,并返回在前端 stdout(1): monitor 正确返回值 输出到前端 stderr(2): monitor 错误返回值 输出到前端

举例说明吧:

当前目录只有一个文件 a.txt.

[root@redhat box]# ls 

a.txt

[root@redhat box]# ls a.txt b.txt 

ls: b.txt: No such file or directory 由于没有b.txt这个文件, 于是返回错误值, 这就是所谓的2输出 a.txt 而这个就是所谓的1输出

再接着看:

[root@redhat box]# ls a.txt b.txt 1>file.out 2>file.err 

执行后,没有任何返回值. 原因是, 返回值都重定向到相应的文件中了,而不再前端显示 [root@redhat box]# cat file.out a.txt [root@redhat box]# cat file.err ls: b.txt: No such file or directory 一般来说, "1>" 通常可以省略成 ">". 即可以把如上命令写成: ls a.txt b.txt >file.out 2>file.err 有了这些认识才能理解 "1>&2" 和 "2>&1". 1>&2 正确返回值传递给2输出通道 &2表示2输出通道 如果此处错写成 1>2, 就表示把1输出重定向到文件2中. 2>&1 错误返回值传递给1输出通道, 同样&1表示1输出通道. 举个例子. [root@redhat box]# ls a.txt b.txt 1>file.out 2>&1 [root@redhat box]# cat file.out ls: b.txt: No such file or directory a.txt 现在, 正确的输出和错误的输出都定向到了file.out这个文件中, 而不显示在前端. 补充下, 输出不只1和2, 还有其他的类型, 这两种只是最常用和最基本的.

是重定向符,就是把前面输出的内容重定向到后面指定的位置,比如(例1):

? 1 echo "一些内容" > filename.txt 上面例子会把 "一些内容" 写入到 filename.txt 文件中。

前是可以加数字来说明把什么内容重定向到文件中,默认是把标准输出重定向到文件中,所以下面这个例子和上面那个是一样的(例2):

? 1 echo "一些内容" 1> filename.txt 如果是错误信息就不会输出到filename.txt(例3):

? 1 2 $ ls nodir 1> filename.txt $ ls: nodir: No such file or directory 上面这个例子中nodir不存在,所以通过ls命令查询时错误信息会输出到 2(stderr),但我们指定的是把1重定向到filename.txt,所以上面命令执行完后,filename.txt中是没有内容的。但是执行下面命令就会把错误信息写入到filename.txt中(例4):

? 1 2 3 $ ls nodir 2> filename.txt $ cat filename.txt $ ls: nodir: No such file or directory & 是一个描述符,如果1或2前不加&,会被当成一个普通文件。

1>&2 意思是把标准输出重定向到标准错误.

2>&1 意思是把标准错误输出重定向到标准输出。

&>filename 意思是把标准输出和标准错误输出都重定向到文件filename中

我们再看一个例子(列5):

? 1 2 3 $ ls nodir 1> filename.txt 2>&1 $ cat filename.txt $ ls: nodir: No such file or directory 上面例子把 标准输出 重定向到文件 filename.txt,然后把 标准错误 重定向到 标准输出,所以最后的错误信息也通过标准输出写入到了文件中,比较例3,4,5,就能明白其作用。

下面是来自百度知道的内容,大家可以参考下:

问:Linux重定向中 >&2 怎么理解? 问题补充:echo "aaaaaaaaaaaaaaaa" >&2 怎么理解?

答:

&2 即 1>&2 也就是把结果输出到和标准错误一样;之前如果有定义标准错误重定向到某log文件,那么标准输出也重定向到这个log文件 如:ls 2>a1 >&2 (等同 ls >a1 2>&1) 把标准输出和标准错误都重定向到a1,终端上看不到任何信息。

https://blog.csdn.net/l1394049664/article/details/81710705

https://blog.csdn.net/qq_31073871/article/details/80810306

ps: 在命令后面加 & 这个符号表示在后台运行


Linux nohup和&的功效

https://www.cnblogs.com/laoyeye/p/9346330.html

Linux中变量#,@,0,1,2,*,$$,$?的含义

https://www.cnblogs.com/jacson/p/4800319.html

shell条件判断if中的-a到-z的意思

[ -a FILE ]  如果 FILE 存在则为真。  
[ -b FILE ]  如果 FILE 存在且是一个块特殊文件则为真。  
[ -c FILE ]  如果 FILE 存在且是一个字特殊文件则为真。  
[ -d FILE ]  如果 FILE 存在且是一个目录则为真。  
[ -e FILE ]  如果 FILE 存在则为真。  
[ -f FILE ]  如果 FILE 存在且是一个普通文件则为真。  
[ -g FILE ] 如果 FILE 存在且已经设置了SGID则为真。 [ -h FILE ]  如果 FILE 存在且是一个符号连接则为真。  
[ -k FILE ]  如果 FILE 存在且已经设置了粘制位则为真。  
[ -p FILE ]  如果 FILE 存在且是一个名字管道(F如果O)则为真。  
[ -r FILE ]  如果 FILE 存在且是可读的则为真。  
[ -s FILE ]  如果 FILE 存在且大小不为0则为真。  
[ -t FD ]  如果文件描述符 FD 打开且指向一个终端则为真。  
[ -u FILE ]  如果 FILE 存在且设置了SUID (set user ID)则为真。  
[ -w FILE ]  如果 FILE 如果 FILE 存在且是可写的则为真。  
[ -x FILE ]  如果 FILE 存在且是可执行的则为真。  
[ -O FILE ]  如果 FILE 存在且属有效用户ID则为真。  
[ -G FILE ]  如果 FILE 存在且属有效用户组则为真。  
[ -L FILE ]  如果 FILE 存在且是一个符号连接则为真。  
[ -N FILE ]  如果 FILE 存在 and has been mod如果ied since it was last read则为真。  
[ -S FILE ]  如果 FILE 存在且是一个套接字则为真。  
[ FILE1 -nt FILE2 ]  如果 FILE1 has been changed more recently than FILE2, or 如果 FILE1 exists and FILE2 does not则为真。  
[ FILE1 -ot FILE2 ]  如果 FILE1 比 FILE2 要老, 或者 FILE2 存在且 FILE1 不存在则为真。  
[ FILE1 -ef FILE2 ]  如果 FILE1 和 FILE2 指向相同的设备和节点号则为真。  
[ -o OPTIONNAME ]  如果 shell选项 “OPTIONNAME” 开启则为真。  
[ -z STRING ]  “STRING” 的长度为零则为真。  
[ -n STRING ] or [ STRING ]  “STRING” 的长度为非零 non-zero则为真。  
[ STRING1 == STRING2 ]  如果2个字符串相同。 “=” may be used instead of “==” for strict POSIX compliance则为真。  
[ STRING1 != STRING2 ]  如果字符串不相等则为真。 
[ STRING1 < STRING2 ]  如果 “STRING1” sorts before “STRING2” lexicographically in the current locale则为真。  
[ STRING1 > STRING2 ]  如果 “STRING1” sorts after “STRING2” lexicographically in the current locale则为真。  
[ ARG1 OP ARG2 ] “OP” is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if “ARG1” is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to “ARG2”, respectively. “ARG1” and “ARG2” are integers.

 

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

基本上和其他脚本语言一样。没有太大区别。不过值得注意的是。[]里面的条件判断。

1、字符串判断

str1 = str2当两个串有相同内容、长度时为真 
str1 != str2 当串str1和str2不等时为真 
-n str1 当串的长度大于0时为真(串非空) 
-z str1 当串的长度为0时为真(空串) 
str1   当串str1为非空时为真

2、数字的判断

int1 -eq int2两数相等为真 
int1 -ne int2两数不等为真 
int1 -gt int2int1大于int2为真 
int1 -ge int2int1大于等于int2为真 
int1 -lt int2int1小于int2为真 
int1 -le int2int1小于等于int2为真

3、文件的判断

-r file用户可读为真 
-w file用户可写为真 
-x file用户可执行为真 
-f file文件为正规文件为真 
-d file文件为目录为真 
-c file文件为字符特殊文件为真 
-b file文件为块特殊文件为真 
-s file文件大小非0时为真 
-t file当文件描述符(默认为1)指定的设备为终端时为真

4、复杂逻辑判断

-a   与 
-o 或 
!非

批量执行接口

在脚本当前目录创建user.txt文件夹,内容如下

张三	13681191111 13681191111@qq.com 12345678

创建一个batch_api.sh的shell脚本

#!/bin/bash
# 批量执行接口

cat user.txt | while read line
do 
	echo $line
	name=`echo $line | awk '{print $1}'`
	username=`echo $line | awk '{print $2}'`
	email=`echo $line | awk '{print $3}'`
	password=`echo $line | awk '{print $4}'`
	
	# skip_confirmation=true 表示不需要邮箱确认
	curl -d "skip_confirmation=true&password=$password&email=$email&username=$username&name=$name&private_token=xxxx" "http://47.95.212.43:20010/api/v4/users"

done

或者

for line in `cat /Users/sktfish/Desktop/api-batch/params.txt`;do 
	
    echo $line
    name=`echo $line | awk '{print $1}'`
    username=`echo $line | awk '{print $2}'`
    email=`echo $line | awk '{print $3}'`
    password=`echo $line | awk '{print $4}'`
	
	curl -H"Cookie:token=ZDNjZDcyN2EtMjdjMy00MTE5LWE5YjgtMmI2ZDI2OTNjMmM0"  -XPOST https://api.sktfish.cn/users/add -d"username=$username&name=$name&email=$email&password=$password";

done
最近更新:: 2025/10/22 15:36
Contributors: luokaiwen