| |

VerySource

 Forgot password?
 Register
Search
View: 793|Reply: 3

[Request assistance] bash shell script problem

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Malaysia

Post time: 2020-9-25 15:30:01
| Show all posts |Read mode
I'm a working student, and I'm a bit busy recently at work, so I can't keep up with the progress of my homework, so I would like to ask everyone to help me solve the homework that I have to hand in on Monday. I am grateful.

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 questions, what I can solve has been solved, I really don't know the remaining 4 questions, so I have to trouble everyone.
Thank you.
Reply

Use magic Report

0

Threads

23

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-9-26 09:30:01
| Show all posts
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
To
#echo $len

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

echo "the max length is $max"
Reply

Use magic Report

0

Threads

23

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-9-26 09:45:02
| Show all posts
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
To
done

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

#echo $newarg
Reply

Use magic Report

0

Threads

23

Posts

15.00

Credits

Newbie

Rank: 1

Credits
15.00

 China

Post time: 2020-9-26 10:00:01
| Show all posts
4
echo "User Name User ID Group ID Home";cat /etc/passwd|awk -F:'{print $1, $3, $4 ,$6}'
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list