bash 6-10

Lesson 6: If statement
#!/bin/bash
MYNUM=100
if [ $MYNUM -eq 200 ]
then
echo “MYNUM is equal to 200.”
else
echo ” MYNUM is not equal to 200”
fi
\=======================================
Lesson 7: Checking the Existense of Files and Folders
*touch myfile(不存在的文件会被create,如果有该文件,会改变最后修改时间)
*rm myfile(删除文件)
*mkdir myfolder(建立新文件夹)
*rm -r myfolder(删除文件夹)

ex:
nano number7 #建立一个名为number7的文件
#!/bin/bash
if [ -d ~/myfolder ] #-d for directory,检查myfolder文件夹是否存在;[ -f ~/myfile ]检查myfile文件是否存在
then
echo “The folder exists.”
else
echo “The folder doesn’t exit.”
end
chmod +x number7 #使number7成为可执行文件
./number7 #运行number7
\=======================================
Lesson 8: Universal Update Script

nano updatescript
#!/bin/bash
cd /etc
# Test if the local host is Arch-based
if [ -d /etc/pacman.d ]
then
# Run the Arch version of the update command
sudo pacman -Syu # Run Arch-based 系统的update代码
fi #close if commond
# Test if the local host is Debian-based
if [ -d /etc/apt ]
then
# Run the Debian version of the update commond
sudo apt-get update && sudo apt-get dist-upgrade -y
fi
\=======================================
Lesson 9:Standard Input, Output, & Error
ls -l
cat
ls -l > filelist.txt #将ls -l的显示内容保存到filelist.txt
ls -l > /dev/null #dev/null是个黑洞,如果扔文件进去,相当于删除了
ls > file.txt #replace file.txt的内容
ls >> file.txt #将内容复制到file.txt结尾
———————————————————————————–
nano standardinputtest
#!/bin/bash
echo “Please enter your name:”
read myname #将输入的文字存入变量myname
echo “You entered: $myname”
—————————————————————————————-
ls /root # 会出error
ls /root 2> errors.txt #0 standard input, 1 standard output, 2 standard error
\=======================================
Lesson 10:Creating a While loop
nano whiletest
#!/bin/bash
myval=1
while [ $myval -le 10 ] n #-le <=
do
echo $myvar
myvar=$(($myvar+1))
sleep 0.5
done
—————————————————————————————————
$0就是该bash文件名
$?是上一指令的返回值
$*所有位置参数的内容:就是调用调用本bash shell的参数。
$@基本上与上面相同。只不过是
“$*”返回的是一个字符串,字符串中存在多外空格。
“$@”返回多个字符串。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值