使用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环境变量,无则添加.