VerySource

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 792|回复: 3

[要求協助]bash shell script問題

[复制链接]

1

主题

1

帖子

2.00

积分

新手上路

Rank: 1

积分
2.00
发表于 2020-9-25 15:30:01 | 显示全部楼层 |阅读模式
我是在職學生, 最近工作有點忙, 所以功課的進度趕不上了, 所以想請大家幫我解決星期一要交上去的功課, 感激不盡.

1. write a script, given a directory as argument (also search for subdirectory), print the longest filename in that directory. ensure the script has error checking. for example, is the argument a directory, not a filename?

2. write a script that finds each line in a file that contains a special substring (not a regexp). to test the script create a file of at least 20 lines. the script should print the result in the following format:

Line Number: Line contents

Do not apply regexp to the whole file but loop over the file line by line.

ensure the script has error checking. for example check that the argument is a file.

3. write a short script that accept any number of arguments, prints the name of the file that is newer. ensure the script has error checking.

4. write a script that creates a table out of the information in the /etc/passwd file using the following format:

User Name       User ID        Group ID          Home
---------       --------       --------          -----
Apple            123            123               /home/apple

4題, 我能解決的都已經解決了, 剩這4題我還真的不太會, 只好麻煩大家.
謝謝.
回复

使用道具 举报

0

主题

23

帖子

15.00

积分

新手上路

Rank: 1

积分
15.00
发表于 2020-9-26 09:30:01 | 显示全部楼层
1
#!/bin/sh
if [ ! $# -eq 1  ] ; then
        echo "The argument is error!"
        echo "example:"
        echo  "        . getlongest.sh /bin"
        return
fi

if [ ! -d $1 ] ; then
        echo "the argument is not a dir"
        return
fi
dir=$1
allfile=`ls -1 $dir`
max=0
maxname=""
for file in $allfile ;  do
        len=`echo $file|wc -c`
        if [ $len -gt $max  ] ; then
                max=$len
                maxname="$file"
        else
                if [ $len -eq $max ] ; then
                        maxname="$maxname $file"
                fi
        fi
       
        #echo $len

done
for maxf in $maxname ; do
        echo $maxf
done

echo "the max length is $max"
回复

使用道具 举报

0

主题

23

帖子

15.00

积分

新手上路

Rank: 1

积分
15.00
发表于 2020-9-26 09:45:02 | 显示全部楼层
3
#!/bin/sh
argument=$*
#echo $*
newarg=""
for arg in $argument; do
        if [ -f $arg ] ; then
                newarg="$newarg $arg"
        else
                echo "$arg is not a file"
        fi
       
done

ls -1t $newarg |sed -n "1,1p"

#echo $newarg
回复

使用道具 举报

0

主题

23

帖子

15.00

积分

新手上路

Rank: 1

积分
15.00
发表于 2020-9-26 10:00:01 | 显示全部楼层
4
echo "User Name User ID Group ID Home";cat /etc/passwd|awk -F: '{print $1, $3, $4 ,$6}'
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

快速回复 返回顶部 返回列表