美文网首页
Shell随笔

Shell随笔

作者: OliverChu | 来源:发表于2019-12-18 20:22 被阅读0次

使用Linux的过程中,我们学会一些批处理是对我们的高效办公有帮助的

if语句:

#!/bin/bash

echo "Please input a integer(0-100):"
read score
if [ "$score"    0 -o "$score" -gt 100 ]
then 
    echo "The score what you input is not a integer or the score is not in (0-100)."
elif [ "$score" -ge 90 ]
then 
    echo "The grade is A."
elif [ "$score" -ge 80 ]
then 
    echo "The grade is B."
elif [ "$score" -ge 70 ]
then 
    echo "The grade is C."
elif [ "$score" -ge 60 ]
then 
    echo "The grade is D."
else
    echo "The grade is E."
fi

案例:

#!/bin/bash
# 启动另一版本的Android Studio
echo "What do you want to do next?"
echo "-1 Run Android Studio 3.0"
echo "-2 Run Android Studio 3.0 background"
read sel
if [ "$sel" -eq 1 ] 
then
    bash /usr/local/android-studio/bin/studio.sh  
    echo "Android studio 3.0 has been started."
elif [ "$sel" -eq 2 ] 
then
    nohup bash /usr/local/android-studio/bin/studio.sh >/dev/null 2>&1 &
    echo "Android studio 3.0 has been started."
fi


以下命令您均可以复制到新建的xx.sh文件中,然后通过chmod +x xx.sh命令赋予可执行权限,以后您可以双击直接运行之。

1.Linux 实现随机壁纸切换

通过getdir函数来遍历文件夹下面的所有文件,并且给填充到a数组中去。

使用$(($RANDOM%$s))来生成一个小于s的随机数,这个数我们作为取的图片文件数组中的索引。使用之后,通过unset arr[idx]来移除数组中索引为idx的元素,同时通过b=("${b[@]}")将元素重新赋值给b,为什么要这样做呢,是因为你会发现移除元素只是将该索引下的元素标记为空,并没有从根本上改变内存大小,因此,我们需要重新赋值。

#!/bin/bash
#@Author: Oliver Chu

#You can set delay time below like this: 1s,1m,1h
delay=10m

#You can set an array of paths below.
path=("/home/oliver/Pictures/Download" "/home/oliver/Pictures/Planets")

a=()
function getdir(){
    for element in `ls $1`
    do  
        dir_or_file=$1"/"$element
        if [ -d $dir_or_file ]
        then 
            getdir $dir_or_file
        else
            a=(${a[@]} $dir_or_file)
        fi  
    done
}

for p in ${path[@]}
do
    getdir $p
done

function run(){
    b=("${a[@]}")
    echo "Quantity of wallpapers:" ${#b[@]}
    s=${#b[@]}
    while [ $s -gt 0 ]
    do
        idx=$(($RANDOM%$s))
        cur=${b[idx]}
        echo `date +%H:%M:%S` $cur
        #For Deepin OS,you can type the command below:
        qdbus --literal com.deepin.wm /com/deepin/wm com.deepin.wm.ChangeCurrentWorkspaceBackground "$cur"
        #For Ubuntu, you can type the command below:
        #gsettings set org.gnome.desktop.background picture-uri "$cur"
        #For other Linux based Operation,you can search "XX 命令换壁纸" via Google, Baidu, etc.
        unset b[idx]
        b=("${b[@]}")
        s=$((s-1))
        sleep $delay
    done
}
while :
do
    run
done
2.Deepin/Ubuntu中查看已连接过的WiFi密码

已经连接过的WiFi热点位于/etc/NetworkManager/system-connections,您只需要查看他们就可以知道密码和加密类型,非常实用。

cd /etc/NetworkManager/system-connections
ls -all
# You can see a list of SSID which you have been connected.
sudo cat {the file you need to cat}
4.某些Linux系统启动不了Android Emulator的时候,使用下列脚本作为一个尝试
#!/bin/bash
printf "ANROID_HOME = " 
if [ "$ANDROID_HOME" =  "" ] 
then
    printf "NULL\n Please input your ANDROID_HOME path:"
    read path 
    export ANDROID_HOME = $path
    printf "I have set ANDROID_HOME ^_^\n\n"
else
    printf "%s\n\n" $ANDROID_HOME
fi
echo "Please select a avd to launch:"
i=0
arr=()
for f in $(emulator -list-avds);do 
    printf "%d %s\n" ${i} ${f}
    arr[i]=${f}
    i+=1
done
printf ">"
read index
printf "What kind of ways do you want to execute?\n"
printf "0 Foreground.\n1 Background.\n>"
read tp
if [$tp = 1]
then
    $ANDROID_HOME/tools/emulator -use-system-libs -avd ${arr[index]}
else
    nohup $ANDROID_HOME/tools/emulator -use-system-libs -avd ${arr[index]} >/dev/null 2>&1 &
fi
printf "Launching avd: %s\n" ${arr[index]}

同样添加执行权限后,双击用终端打开,选择即可,会判断你是否设置ANDROID_HOME环境变量,无则添加.

相关文章

  • shell随笔

    shell基本概念 解释器类型bash是centos中的默认解释器sh 脚本定义 !/bin/bash其中#!叫做...

  • Shell随笔

    使用Linux的过程中,我们学会一些批处理是对我们的高效办公有帮助的 if语句: 案例: 以下命令您均可以复制到新...

  • shell 随笔

    1、manmanual 是类 UNIX 系统的标准手册工具,共 9 卷,用 man man可以看到 9 卷分别是什...

  • Shell 学习

    shell 变量 shell 参数传递 shell 数组 shell 运算符 shell echo 命令 prin...

  • Shell 概述

    学习 Shell 主要包括的内容: Shell 脚本入门 Shell 变量 Shell 内置命令 Shell 运算...

  • Shell 教程

    Shell 变量 Shell 传递参数 Shell 数组 Shell 基本运算符 Shell echo 命令 Sh...

  • shell 第一天

    shell编程初识 1.1 shell编程初识 shell的定义 Shell 是命令解释器 Shell 也是...

  • shell 案例

    Shell编程一 Shell防范ARP攻击 Shell编程二 Shell防范DDos攻击 Shell编程三 ...

  • 【生物信息笔记】shell 脚本 (dry-2)

    shell 和 shell script(脚本)区别: shell 和 shell 脚本是两个不同概念,shell...

  • Linux Shell:基础知识和Shell变量

    摘要:Linux,Shell 整理Shell内容要点: Shell基础知识 Shell变量的类型 Shell变量赋...

网友评论

      本文标题:Shell随笔

      本文链接:https://www.haomeiwen.com/subject/bcxpnctx.html