Q:1 Shell脚本是什么、它是必需的吗?
Q:2 什么是默认登录shell,如何改变指定用户的登录shell
# chsh <username> -s <new_default_shell>
# chsh linuxtechi -s /bin/sh
Q:3 可以在shell脚本中使用哪些类型的变量?
用户变量由系统用户来生成和定义,变量的值可以通过命令“echo $<变量名>”查看。
Q:4 如何将标准输出和错误输出同时重定向到同一位置?
方法一:
2>&1 (# ls /usr/share/doc > out.txt 2>&1 )
方法二:
&> (# ls /usr/share/doc &> out.txt )
Q:5 shell脚本中“if”语法如何嵌套?
if [ Condition ]
then
command1
command2
…..
else
if [ condition ]
then
command1
command2
….
else
command1
command2
…..
fi
fi
Q:6 shell脚本中“$?”标记的用途是什么? ?
root@localhost:~# ls /usr/bin/shar
/usr/bin/shar
root@localhost:~# echo $?
0
如果结束状态是0,说明前一个命令执行成功。
root@localhost:~# ls /usr/bin/share
ls: cannot access /usr/bin/share: No such file or directory
root@localhost:~# echo $?
2
如果结束状态不是0,说明命令执行失败。
Q:7 在shell脚本中如何比较两个数字 ?
#!/bin/bash
x=10
y=20
if [ $x -gt $y ]
then
echo “x is greater than y”
else
echo “y is greater than x”
fi
Q:8 shell脚本中break命令的作用 ?
Q:9 shell脚本中continue命令的作用 ?
Q:10 告诉我shell脚本中Case语句的语法 ?
case word in
value1)
command1
command2
…..
last_command
!!
value2)
command1
command2
……
last_command
;;
esac
Q:11 shell脚本中while循环语法 ?
while [ test_condition ]
do
commands…
done
12 如何使脚本可执行 ?
# chmod a+x myscript.sh
1
# chmod a+x myscript.sh
Q:13 “#!/bin/bash”的作用 ?
Q:14 shell脚本中for循环语法 ?
for variables in list_of_items
do
command1
command2
….
last_command
done
Q:15 如何调试shell脚本 ?
Q:16 shell脚本如何比较字符串?
Q:17 Bourne shell(bash) 中有哪些特殊的变量 ?
内建变量 | 解释 |
---|---|
$1 | 命令行中的脚本名字 |
$1 | 第一个命令行参数 |
$2 | 第二个命令行参数 |
…… | …… |
$9 | 第九个命令行参数 |
$# | 命令行参数的数量 |
$* | 所有命令行参数,以空格隔开 |
Q:18 在shell脚本中,如何测试文件 ?
Test | 用法 |
---|---|
-d 文件名 | 如果文件存在并且是目录,返回true |
-e 文件名 | 如果文件存在,返回true |
-f 文件名 | 如果文件存在并且是普通文件,返回true |
-r 文件名 | 如果文件存在并可读,返回true |
-s 文件名 | 如果文件存在并且不为空,返回true |
-w 文件名 | 如果文件存在并可写,返回true |
-x 文件名 | 如果文件存在并可执行,返回true |
Q:19 在shell脚本中,如何写入注释 ?
#!/bin/bash
# This is a command
echo “I am logged in as $USER”
Q:20 如何让 shell 就脚本得到来自终端的输入?
# vi /tmp/test.sh
#!/bin/bash
echo ‘Please enter your name’
read name
echo “My Name is $name”
# ./test.sh
Please enter your name
LinuxTechi
My Name is LinuxTechi
Q:21 如何取消变量或取消变量赋值 ?
# unset <Name_of_Variable>
Q:22 如何执行算术运算 ?
使用expr命令(# expr 5 + 2)
2.用一个美元符号和方括号($[ 表达式 ])
例如:test=$[16 + 4]
; test=$[16 + 4]
Q:23 do-while语句的基本格式 ?
do
{
statements
} while (condition)
Q:24 在shell脚本如何定义函数呢 ?
$ diskusage () { df -h ; }
Q:25 如何在shell脚本中使用BC(bash计算器) ?
variable=`echo “options; expression” | bc`
原文链接:http://blog.jobbole.com/86237/