云计算基础之一 Shell脚本编写

  1. Write a Shell script that accepts a filename, starting and ending line numbers as arguments and displays all the lines between the given line numbers.
    在这里插入图片描述
    Script:
#! /bin/sh
sed -n $2,$3p $1
  1. Write a Shell script that deletes all lines containing a specified word in one or more files supplied as arguments to it.
    在这里插入图片描述
    Script:
#!/bin/bash
word="$1"
let paracount=$#-1
let counter=1
while (( $paracount >= $counter)); do
	let counter+=1
	shift 1
	sed -i "/$word/d" $1
done
  1. Write a Shell script that displays list of all the files in the current directory to which the user has read, Write and execute permissions.
    在这里插入图片描述
    Script:
#!/bin/bash
for i in `ls`; {
        if [[ -r $i ]]&&[[ -w $i ]]&&[[ -x $i ]];
        then echo $i;
fi;}
  1. Write a Shell script that receives any number of file names as arguments checks if every argument supplied is a file or a directory and reports accordingly. Whenever the argument is a file, the number of lines on it is also reported.
    在这里插入图片描述
    Script:
#!/bin/bash
echo $1
let paracount=$#
let counter=1
while (( $paracount >= $counter )); do
	let counter+=1
        shift 1
	if [ -d  $value ];
        then echo $value--directory;
	fi

	if [ -f $value ];
        then echo $value--file;
	     awk 'END{print NR}' $value;
	fi
done
  1. Write a Shell script that accepts a list of file names as its arguments, counts and reports the occurrence of each word that is present in the first argument file on other argument files.
    在这里插入图片描述
    Script:
#!/bin/bash
declare -A word_list
for word in `cat $1|sed 's/[,.:;/!?()_{}+-=*]/ /g'`
do
        word_list[$word]=0
done
shift 1
for file in $*
do
        for word in `cat $1|sed 's/[,.:;/!?()_{}+-=*]/ /g'`
            do
                for key in ${!word_list[\*]}
                    do
                        if [[ $word =~ $key ]]; then
                             word_list[$key]=$((${word_list[$key]}+1))
                        fi
                done
        done
done
for key in ${!word_list[*]}
do
        echo $key": "${word_list[$key]}
done
  1. Write a Shell script to list all of the directory files in a directory.
    在这里插入图片描述
    Script:
#!/bin/bash
judge( ){
for i in `ls`; { 
        if [ -d  $i ];
        then echo $i--directory; 
             cd $i;
	     judge
fi;}
cd ..
}
judge
  1. Write a Shell script to find factorial of a given integer.
    在这里插入图片描述
    Script:
#!/bin/bash
num=$1
[ $? -ne 0 ] && echo "please input a number" && exit 1
[ $# -ne 1 ] && echo 'please input a number' && exit 1
[ $num -le 0 ] && echo "please input a number bigger than 0" && exit 2
s=1
for i in `seq 1 $num`
do 
     s=$(($s*$i))
done
echo $s
  1. Script for script
    在这里插入图片描述
    Script:
#!/bin/bash
bash ./shell1.sh poem.txt 1 2;
bash ./shell2.sh function poem.txt;
bash ./shell3.sh;
bash ./shell4.sh shell3.sh test;
bash ./shell5.sh poem.txt poem.txt;
bash ./shell6.sh;
exec ./shell7.sh 5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值