脚本专栏 
首页 > 脚本专栏 > 浏览文章

Linux Shell脚本面试25问集合

(编辑:jimmy 日期: 2024/9/17 浏览:3 次 )

Q:1 Shell脚本是什么、它是必需的吗"toc_2">Q:2 什么是默认登录shell,如何改变指定用户的登录shell

答:在Linux操作系统,“/bin/bash”是默认登录shell,是在创建用户时分配的。使用chsh命令可以改变默认的shell。示例如下所示:

# chsh <username> -s <new_default_shell>
# chsh linuxtechi -s /bin/sh

Q:3 可以在shell脚本中使用哪些类型的变量"toc_4">Q:4 如何将标准输出和错误输出同时重定向到同一位置"brush: bash; gutter: true"> if [ Condition ] then command1 command2 ….. else if [ condition ] then command1 command2 …. else command1 command2 ….. fi fi

Q:6 shell脚本中“$"brush: bash; gutter: true"> root@localhost:~# ls /usr/bin/shar /usr/bin/shar root@localhost:~# echo $"brush: bash; gutter: true"> root@localhost:~# ls /usr/bin/share ls: cannot access /usr/bin/share: No such file or directory root@localhost:~# echo $"toc_7">Q:7 在shell脚本中如何比较两个数字 "brush: bash; gutter: true"> #!/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命令的作用 "toc_9">Q:9 shell脚本中continue命令的作用 "toc_10">Q:10 告诉我shell脚本中Case语句的语法 "brush: bash; gutter: true"> case word in value1) command1 command2 ….. last_command !! value2) command1 command2 …… last_command ;; esac

Q:11 shell脚本中while循环语法 "brush: bash; gutter: true"> while [ test_condition ] do commands… done

Q:12 如何使脚本可执行 "brush: actionscript3; gutter: true"> # chmod a+x myscript.sh

Q:13 “#!/bin/bash”的作用 "toc_14">Q:14 shell脚本中for循环语法 "brush: bash; gutter: true"> for variables in list_of_items do command1 command2 …. last_command done

Q:15 如何调试shell脚本 "toc_16">Q:16 shell脚本如何比较字符串"toc_17">Q:17 Bourne shell(bash) 中有哪些特殊的变量 "0" cellpadding="4" width="659" border="1">

内建变量

解释

$0

命令行中的脚本名字

$1

第一个命令行参数

$2

第二个命令行参数

…..

…….

$9

第九个命令行参数

$#

命令行参数的数量

$*

所有命令行参数,以空格隔开

Q:18 How to test files in a shell script "toc_19">Q:18 在shell脚本中,如何测试文件 "0" cellpadding="4" border="1">

Test

用法

-d 文件名

如果文件存在并且是目录,返回true

-e 文件名

如果文件存在,返回true

-f 文件名

如果文件存在并且是普通文件,返回true

-r 文件名

如果文件存在并可读,返回true

-s 文件名

如果文件存在并且不为空,返回true

-w 文件名

如果文件存在并可写,返回true

-x 文件名

如果文件存在并可执行,返回true

Q:19 在shell脚本中,如何写入注释 "brush: bash; gutter: true"> #!/bin/bash # This is a command echo “I am logged in as $USER”

Q:20 如何让 shell 就脚本得到来自终端的输入"brush: bash; gutter: true"> # 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 如何取消变量或取消变量赋值 "brush: bash; gutter: true"> # unset <Name_of_Variable>

Q:22 如何执行算术运算 "toc_24">Q:23 do-while语句的基本格式 "brush: bash; gutter: true"> do { statements } while (condition)

Q:24 在shell脚本如何定义函数呢 "brush: bash; gutter: true"> $ diskusage () { df -h ; }

Q:25 如何在shell脚本中使用BC(bash计算器) "brush: bash; gutter: true"> variable=`echo “options; expression” | bc`

原文出处: linuxtechi   译文出处:LCTT

上一篇:Linux中使用expect脚本实现远程机器自动登录
下一篇:linux下ssh安装与scp命令使用详解
一句话新闻
一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 SiteMap