shell 脚本程序Demo

1.算数运算符

#!/bin/sh
a=10
b=20
val=`expr $a + $b`
echo "a + b : $val"

val=`expr $a - $b`
echo "a - b : $val"

val=`expr $a \* $b`
echo "a * b : $val"

val=`expr $b / $a`
echo "b / a : $val"

val=`expr $b % $a`
echo "b % a : $val"

if [ $a == $b ]
then
   echo "a is equal to b"
fi

if [ $a != $b ]
then
   echo "a is not equal to b"
fi

2 .关系运算符

#!/bin/sh

a=10
b=20
if [ $a -eq $b ]
then
   echo "$a -eq $b : a is equal to b"
else
   echo "$a -eq $b: a is not equal to b"
fi

if [ $a -ne $b ]
then
   echo "$a -ne $b: a is not equal to b"
else
   echo "$a -ne $b : a is equal to b"
fi

if [ $a -gt $b ]
then
   echo "$a -gt $b: a is greater than b"
else
   echo "$a -gt $b: a is not greater than b"
fi

if [ $a -lt $b ]
then
   echo "$a -lt $b: a is less than b"
else
   echo "$a -lt $b: a is not less than b"
fi

if [ $a -ge $b ]
then
   echo "$a -ge $b: a is greater or  equal to b"
else
   echo "$a -ge $b: a is not greater or equal to b"
fi

if [ $a -le $b ]
then
   echo "$a -le $b: a is less or  equal to b"
else
   echo "$a -le $b: a is not less or equal to b"
fi


3. 布尔运算符

#!/bin/sh

a=10
b=20

if [ $a != $b ]
then
   echo "$a != $b : a is not equal to b"
else
   echo "$a != $b: a is equal to b"
fi

if [ $a -lt 100 -a $b -gt 15 ]
then
   echo "$a -lt 100 -a $b -gt 15 : returns true"
else
   echo "$a -lt 100 -a $b -gt 15 : returns false"
fi

if [ $a -lt 100 -o $b -gt 100 ]
then
   echo "$a -lt 100 -o $b -gt 100 : returns true"
else
   echo "$a -lt 100 -o $b -gt 100 : returns false"
fi

if [ $a -lt 5 -o $b -gt 100 ]
then
   echo "$a -lt 100 -o $b -gt 100 : returns true"
else
   echo "$a -lt 100 -o $b -gt 100 : returns false"
fi

4.字符串运算符

#!/bin/sh

a="abc"
b="efg"

if [ $a = $b ]
then
   echo "$a = $b : a is equal to b"
else
   echo "$a = $b: a is not equal to b"
fi

if [ $a != $b ]
then
   echo "$a != $b : a is not equal to b"
else
   echo "$a != $b: a is equal to b"
fi

if [ -z $a ]
then
   echo "-z $a : string length is zero"
else
   echo "-z $a : string length is not zero"
fi

if [ -n $a ]
then
   echo "-n $a : string length is not zero"
else
   echo "-n $a : string length is zero"
fi

if [ $a ]
then
   echo "$a : string is not empty"
else
   echo "$a : string is empty"
fi

5.文件测试运算符

#!/bin/sh

file="/var/www/tutorialspoint/unix/test.sh"

if [ -r $file ]
then
   echo "File has read access"
else
   echo "File does not have read access"
fi

if [ -w $file ]
then
   echo "File has write permission"
else
   echo "File does not have write permission"
fi

if [ -x $file ]
then
   echo "File has execute permission"
else
   echo "File does not have execute permission"
fi

if [ -f $file ]
then
   echo "File is an ordinary file"
else
   echo "This is sepcial file"
fi

if [ -d $file ]
then
   echo "File is a directory"
else
   echo "This is not a directory"
fi

if [ -s $file ]
then
   echo "File size is zero"
else
   echo "File size is not zero"
fi

if [ -e $file ]
then
   echo "File exists"
else
   echo "File does not exist"
fi

6.遍历数组

#!/bin/bash

array=(a b c d)
for arr in ${array[@]}
do
echo $arr
done

7.ifelse语句

#!/bin/sh

a=10
b=20

if [ $a == $b ]
then
   echo "a is equal to b"
elif [ $a -gt $b ]
then
   echo "a is greater than b"
elif [ $a -lt $b ]
then
   echo "a is less than b"
else
   echo "None of the condition met"
fi

8.while循环

#!/bin/bash

COUNTER=0
while [ $COUNTER -lt 5 ]
do
    COUNTER='expr $COUNTER + 1'
    echo $COUNTER
done


echo 'type <CTRL-D> to terminate'
echo -n 'enter your most liked film: '
while read FILM
do
    echo "Yeah! great film the $FILM"
done

9.for循环

for loop in 1 2 3 4 5
do
    echo "The value is: $loop"
done

10.函数

#!/bin/bash
funSum(){
    echo "The function is to get the sum of two numbers..."
    echo -n "Input first number: "
    read aNum
    echo -n "Input another number: "
    read bNum
    echo "The two numbers are $aNum and $bNum "
    return `expr $aNum + $bNum`
}
funSum

# Capture value returnd by last command
ret=$?
echo "The sum of two numbers is $ret !"

#!/bin/bash
funWithParam(){
    echo "The value of the first parameter is $1 !"
    echo "The value of the second parameter is $2 !"
    echo "The value of the tenth parameter is ${10} !"
    echo "The value of the eleventh parameter is ${11} !"
    echo "The amount of the parameters is $# !"  # 参数个数
    echo "The string of the parameters is $* !"  # 传递给函数的所有参数
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73

11.java程序启动脚本示例

#!/bin/bash

#-----------------------------------------------
#Author:test
#
#-----------------------------------------------

JVM_OPTIONS="-server -Xms20m -Xmx20m -XX:MaxPermSize=20M 
  -XX:+HeapDumpOnOutOfMemoryError"

#
#帮助信息输出函数
#
usage(){
  echo "用法: ./appdemo.sh [options] 
其中的选项包括:
    --start 启动app
    --stop  停止app"
}

error_info(){
  echo $1
}

start(){
  JVM_OPTIONS=$JVM_OPTIONS" -Duser.dir=$APPDEMO_HOME"
  echo "jvm options:$JVM_OPTIONS"
  java $JVM_OPTIONS -jar $APPDEMO_HOME/app-demo.jar start  
}

stop(){
  java -jar $APPDEMO_HOME/app-demo.jar stop
}

casage(){
  case $1 in
        --help)
            usage
        ;;
        --start)
          start
        ;;
        --stop)
          stop
        ;;
        *)
        error_info "错误,找不到该选项,请使用--help察看可用选项"
        ;;
    esac
}

if [ -z $APPDEMO_HOME  ]
 then
  APPDEMO_BIN=`pwd`
  APPDEMO_HOME=$(dirname $APPDEMO_BIN)
fi

if [ $# == 0  ] || [ $# -gt 1  ]
 then 
    usage
else
    command=$1
    casage $command
fi


转载于:https://my.oschina.net/bigdataer/blog/539983

Shell脚本高级编程教程,希望对你有所帮助。 Example 10-23. Using continue N in an actual task: 1 # Albert Reiner gives an example of how to use "continue N": 2 # --------------------------------------------------------- 3 4 # Suppose I have a large number of jobs that need to be run, with 5 #+ any data that is to be treated in files of a given name pattern in a 6 #+ directory. There are several machines that access this directory, and 7 #+ I want to distribute the work over these different boxen. Then I 8 #+ usually nohup something like the following on every box: 9 10 while true 11 do 12 for n in .iso.* 13 do 14 [ "$n" = ".iso.opts" ] && continue 15 beta=${n#.iso.} 16 [ -r .Iso.$beta ] && continue 17 [ -r .lock.$beta ] && sleep 10 && continue 18 lockfile -r0 .lock.$beta || continue 19 echo -n "$beta: " `date` 20 run-isotherm $beta 21 date 22 ls -alF .Iso.$beta 23 [ -r .Iso.$beta ] && rm -f .lock.$beta 24 continue 2 25 done 26 break 27 done 28 29 # The details, in particular the sleep N, are particular to my 30 #+ application, but the general pattern is: 31 32 while true 33 do 34 for job in {pattern} 35 do 36 {job already done or running} && continue 37 {mark job as running, do job, mark job as done} 38 continue 2 39 done 40 break # Or something like `sleep 600' to avoid termination. 41 done 42 43 # This way the script will stop only when there are no more jobs to do 44 #+ (including jobs that were added during runtime). Through the use 45 #+ of appropriate lockfiles it can be run on several machines 46 #+ concurrently without duplication of calculations [which run a couple 47 #+ of hours in my case, so I really want to avoid this]. Also, as search 48 #+ always starts again from the beginning, one can encode priorities in 49 #+ the file names. Of course, one could also do this without `continue 2', 50 #+ but then one would have to actually check whether or not some job 51 #+ was done (so that we should immediately look for the next job) or not 52 #+ (in which case we terminate or sleep for a long time before checking 53 #+ for a new job).
### 回答1: 可以使用shell脚本删除列,下面是一个demo:cut -d, -f1,2,4,5,6,7,8,9,10,11,12,13,14 - --output-delimiter=" " test.csv > test_clean.csv ### 回答2: shell脚本删除列demo可以通过使用awk命令来实现。awk是一种用于处理文本的流式编辑器,可以很方便地操作文本文件的列。 以下是一个示例的shell脚本删除列demo: ```shell #!/bin/bash # 假设我们有一个名为data.txt的文本文件,内容如下: # 列1 列2 列3 列4 # 1 2 3 4 # A B C D # 要删除第3列,可以使用awk命令,并将结果重定向到一个新文件中 awk '{$3=""; print}' data.txt > new_data.txt # 使用awk命令,将每一行的第3列的内容设置为空,然后打印整行。结果将被重定向到新的文件new_data.txt中。 # 执行脚本后,new_data.txt 文件将会是如下内容: # 列1 列2 列3 列4 # 1 2 4 # A B D # 第3列已被删除,原来的数据文件data.txt并不会改变。 ``` 这是一个简单的shell脚本示例,用于演示如何通过使用awk命令来删除列。你可以根据需要修改列的位置和文件名来删除不同的列。 ### 回答3: shell脚本删除列demo可以使用awk命令来完成。以下是一个简单的示例: 假设有一个名为input.txt的文件,文件内容如下: ``` 姓名,性别,年龄 张三,男,25 李四,女,30 王五,男,35 ``` 现在我们想要删除第二列(性别)。可以编写一个shell脚本如下: ```bash #!/bin/bash # 定义输入文件名和输出文件名 input_file="input.txt" output_file="output.txt" # 使用awk命令删除第二列,并将结果输出到output.txt文件中 awk 'BEGIN {FS=OFS=","} { $2 = ""; print $0 }' "$input_file" > "$output_file" ``` 脚本中,我们首先定义了输入文件名`input.txt`和输出文件名`output.txt`。然后使用awk命令,通过设置输入和输出的字段分隔符为逗号`FS=OFS=","`,将第二列设为空字符串`$2 = ""`,然后打印整行`print $0`。最后将结果输出到`output.txt`文件中。 运行脚本后,`input.txt`中的内容将会被处理,删除第二列,并将结果保存到`output.txt`中。输出文件的内容如下: ``` 姓名,,年龄 张三,,25 李四,,30 王五,,35 ``` 注意,原始文件`input.txt`并没有被改动,只有输出文件`output.txt`被生成和修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值