centos中的shell编程

1.shell 是批处理程序,类似与windows的bat文件
2.写shell时,第一行要以#!/bin/bash 开头 Execute the file using the Bash shell.
3.使用#注释(最好备注shell脚本的功能作用以防日后忘记)
4.文件名应以.sh结尾
5.运行时,执行方式 sh 1.sh;chmod +x 1.sh; ./1.sh || /root/test/1.sh(绝对路径)
6.$? //命令的返回值存储变量
$# //参数个数
$1 //第几个参数。提取参数
$0 //当前脚本命令的名称
$@ //取出所有参数
$shift //参数左移
7.1)建立第一个脚本文件,
$>touch a.sh;//创建脚本文件,创建脚本文件之后修改文件权限,所有人都可以执行该文件,chmod a+x a.sh
$>#!/bin/bash
echo hello world
这个程序就会打印除hello world
7.2)#!/bin/bash
num=$#
echo num >>1.txt //这个脚本打印出输入参数的个数
7.3)#!/bin/bash
echo helloworld!
echo parameters is $#!
echo script's name is $0.
7.4)
#!/bin/bash
echo $1.
shift.
echo $1.
shift.
echo $1.
shift.
echo $1.
shift.

8.if[$# -lt 1] //这句话的意思是如果参数个数小于1
if[$# -gt 1] //这句话的意思是如果参数个数大于1
if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
for ((: for (( exp1; exp2; exp3 )); do COMMANDS; done
8.1)#!/bin/bash
num= $@ 取出所有参数
for(( i = 1 ; i <= num ; i = $i+1)) ;do
for((y = 1 ; y <= x ; x= $x+1 )); do
echo -n $y;
done
echo ;
done
8.2)九九乘法表
#!/bin/bash
i=1
line=$1
while(( i<= $line )) ; do
j=1
while(( j<$i )) ; do
echo -ne ${j}x${i}=$(( j*i))' ';
j=$(( j+1 ));
done ;
i=$(( i+1 ))
echo ;
done;

原文地址:https://www.cnblogs.com/stone-learning/p/9296885.html