当前位置: 首页 > 图文教程 > 操作系统 > Unix/Linux > Linux程式设计-11.ShellScript(bash)--(2)教学例

Unix/Linux
linux查看内存的大小
在linux下写的代码,用的是utf-8,结果拿到XP下运行的时候,所有的中文都成乱码
linux su和sudo命令的区别
linux cron 下的定时执行工具使用技巧
linux 查找进程及终止进程操作的相关命令
redhat linux 安装 gcc编译器
Linux Mplayer播放各种格式的电影
一起回顾一下linux常用命令
Linux 网站项目发布要做哪些配置
linux SSH配合SecureCRT的密匙完美使用方法
GD 编译出错解决方法
Facebook Open Platform编译FAQ
Linux 系统硬盘 优化
linux 挂载详解
linux crontab定时命令
Linux 系统中确保访问三级域名畅通的方法
Linux 特权帐号VS普通帐号
确保Linux系统安全的前提条件 漏洞防护
Linux 监视系统资源使用率
Red Hat Linux上使用BIND建立DNS服务器

Unix/Linux 中的 Linux程式设计-11.ShellScript(bash)--(2)教学例


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-01   浏览: 35 ::
收藏到网摘: n/a


  "Hello world" Shell Script
  照传统程式教学例,这一节介绍Shell Script的"Hello World"如何撰写。
  
  
  
  --------------------------------------------------------------------------------
  
  #!/bin/sh
  # Filename : hello
  echo "Hello world!"
  
  --------------------------------------------------------------------------------
  
  大家应该会注意到第一行的"#!/bin/sh"。在UNIX下,所有的可执行Script,不管是那一种语言,其开头都是"#!",例如Perl是"#!/usr/bin/perl",tcl/tk是"#!/usr/bin/wish",看您要执行的Script程式位置在那里。您也可以用"#!/bin/bash"、"#!/bin/tcsh"等等,来指定使用特定的Shell。
  echo是个bash的内建指令。
  
  
  
  --------------------------------------------------------------------------------
  
  接下来,执行hello这个script:
  要执行一个Script的方式有很多种。
  
  
  
  --------------------------------------------------------------------------------
  
  第一种 : 将hello这个档案的权限设定为可执行。
  [foxman@foxman bash]# chmod 755 hello

  执行
  [foxman@foxman bash]# ./hello
  hello world
  
  
  
  --------------------------------------------------------------------------------
  
  第二种 : 使用bash内建指令"source"或"."。
  [foxman@foxman bash]# source hello
  hello world
  或
  [foxman@foxman bash]# . hello
  hello world
  
  
  
  --------------------------------------------------------------------------------
  
  第三种 : 直接使用sh/bash/tcsh指令来执行。
  [foxman@foxman bash]# sh hello
  hello world
  或
  [foxman@foxman bash]# bash hello
  hello world
  
  
  
  --------------------------------------------------------------------------------
  
  Bash执行选项
  
  --------------------------------------------------------------------------------
  
  -c string : 读取string来当命令。
  -i : 互动介面。
  -s : 由stdin读取命令。
  - : 取消往後选项的读取。
  -norc : 不要读~/.bashrc来执行。
  -noprofile : 不要读/etc/profile、~/.bash_profile、~/.bash_login、~/.profile等等来执行。
  -rcfile filename : 执行filename,而非~/.bashrc
  -version : 显示版本。
  -quiet : 启动时不要哩唆。
  -login : 确保bash是个login shell。
  -nobraceexpansion : 不要用curly brace expansion({}符号展开)。
  -nolineediting : 不用readline来读取命令列。
  -posix : 改采Posix 1003.2标准。