linux Shell的使用

下面是我对shell脚本使用的学习,写成日志,做个笔记,可以当做自己以后的复习。

1.输出hello world!

vi test1.sh, 如果不会vi工具的使用,请参考 linux vi 工具的使用

#!/bin/bash
echo "hello world"

注:新创建的文件默认是没有执行权限的,所以必须给该文件授予执行权限后才能执行。授权命令:chmod u+x test1.sh或者chmod 755 test1.sh

最后执行

./test1.sh

2.使用变量

vi test2.sh

#!/bin/bash
read -p "please input your name:" name
echo "hello,$name!"

3.条件判断的使用

vi condition.sh

#!/bin/bash
if [ "$1" = "normal" ]
then
echo "this is normal case"
elif [ -z "$1" ]
then
echo "no input, ignal..."
fi

./condition.sh normal

输出:this is normal case  

注意: [ 符号后面必须有一个空格,否则执行出错。 

4. whle[]...do...done 语句

类似于c语言的do..while 语句

#!/bin/bash
echo "please use add or delete or exit"
ACTION="default"
while [ -n $ACTION ]
do
read ACTION
case $ACTION in
add)
echo "add somebody"
;;
delete)
echo "delete somebody"
;;
exit)
echo "complete"
break
;;
*)
echo "invalide action,please re-enter"
;;
esac
done

 

5 for循环

#!/bin/bash
for C in 123 2 3 4 5 1 hello
do
echo $C
done

 输出每一个元素 

6 函数的使用

#!/bin/bash
strcat()
{
OUT="$1"" ""$2"
return 0
} stract2()
{
echo "$1"" ""$2"
return 3
} A="bird"
B="mouse" OUT=""
strcat $A $B
echo $OUT
OUT2='strcat2 $A $B'
echo $?
echo $OUT2

输出结果:

bird mouse
0
strcat2 bird mouse

参考资料

shell语法使用

Android内核剖析

好吧,对shell的学习先到这里,以后继续深入学习Shell。可以使用这本书 《Linux于unix shell编程指南》。

linux Shell的使用的更多相关文章

  1. linux shell 中的sleep命令

    开始还以为是这样的语法: sleep(1), 后面发现是: linux shell 中的sleep命令 分类: LINUX 在有的shell(比如linux中的bash)中sleep还支持睡眠(分,小 ...

  2. Linux shell脚本编程(三)

    Linux shell脚本编程 流程控制: 循环语句:for,while,until while循环: while CONDITION; do 循环体 done 进入条件:当CONDITION为“真” ...

  3. Linux shell脚本编程(二)

    Linux shell脚本编程(二) 练习:求100以内所有偶数之和; 使用至少三种方法实现; 示例1: #!/bin/bash # declare -i sum=0 #声明一个变量求和,初始值为0 ...

  4. Linux shell脚本编程(一)

    Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...

  5. Linux Shell 流程控制语句

    * 本文主要介绍一些Linux Shell 常用的流程控制语句* 1. if 条件语句:if-then/if-elif-fi/if- else-fi if [条件判断逻辑1];then command ...

  6. Linux Shell 截取字符串

    Linux Shell 截取字符串 shell中截取字符串的方法很多 ${var#*/} ${var##*/} ${var%/*} ${var%%/*} ${var:start:len} ${var: ...

  7. Linux Shell 重定向与管道【转帖】

    by 程默 在了解重定向之前,我们先来看看linux 的文件描述符. linux文件描述符:可以理解为linux跟踪打开文件,而分配的一个数字,这个数字有点类似c语言操作文件时候的句柄,通过句柄就可以 ...

  8. Linux Shell 通配符、元字符、转义符【转帖】

    作者:程默 说到shell通配符(wildcard),大家在使用时候会经常用到.下面是一个实例: 1   1 2 3 4 [chengmo@localhost ~/shell]$ ls a.txt  ...

  9. Linux Shell中单引号、双引号、反引号的区别【转载】

    linux shell可以识别4种不同类型的引字符号: 单引号字符' 双引号字符" 反斜杠字符\ 反引号字符` 1. 单引号 ( '' )# grep Susan phonebook Sus ...

  10. 【shell 大系】Linux Shell常用技巧

    在最近的日常工作中由于经常会和Linux服务器打交道,如Oracle性能优化.我们数据采集服务器的资源利用率监控,以及Debug服务器代码并解决其效率和稳定性等问题.因此这段时间总结的有关Linux ...

随机推荐

  1. 如何给magento的产品页面添加返回按钮

    如何给magento的产品页面添加返回按钮,最模板提供教程 第一步: 打开 E:\xampp\htdocs\magento\skin\frontend\default\bluescale\css\st ...

  2. usb serial for android

    /******************************************************************** * usb serial for android * 说明: ...

  3. poj 1990 MooFest

    题目大意: FJ有n头牛,排列成一条直线(不会在同一个点),给出每头牛在直线上的坐标x.另外,每头牛还有一个自己的声调v,如果两头牛(i和j)之间想要沟通的话,它们必须用同个音调max(v[i],v[ ...

  4. Tomcat发布网站知识集锦

    修改端口.修改默认发布目录.多域名绑定 一.修改发布端口号为80(Tomcat默认为8080) 打开配置文件(我的如下:E:\J2EEServer\Tomcat 6.0\conf\server.xml ...

  5. 关于C中scanf()函数读取字符串的问题

    #include <stdio.h> int main(void) { ]; scanf("%s", s_name); printf("Hello, %s!\ ...

  6. init/main.c

    /* * linux/init/main.c * * Copyright (C) 1991, 1992 Linus Torvalds */ #include <stdarg.h> #inc ...

  7. ubuntu 以root 运行程序

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  8. URAL(timus) 1280 Topological Sorting(模拟)

    Topological Sorting Time limit: 1.0 secondMemory limit: 64 MB Michael wants to win the world champio ...

  9. 一些基本的C/C++数据类型

    size_t size_t. A basic unsigned integer C/C++ type. It is the type of the result returned by sizeof ...

  10. 回调函数的实现 & 结构体的继承

    ------------------------------------------------------------------------------------[1]------------- ...