Part 1
You need to write two shell scripts, both of them should be executable in a Linux shell.
-
s1.sh
:% ./s1.sh foo
The script first builds a new directory
foo
in the current directory (the one containssh1.sh
), then creates two files: "name.txt" containing your name; "stno.txt" containing your student number. Finally, it makes a copy of "name.txt" and "stno.txt" in the same directory offoo
. -
s2.sh
:
It generates a text file% ./s2.sh
output
. Each row ofoutput
represents a file in directory/bin
, and contains three fields which are name, owner and permission of that file. The fields should be separated by white spaces. We further require that- All file names in
output
should start with letter "b" (other file names should be excluded) - The file
output
is sorted by the file name field (in order of alphabet, from small to large) - File
output
is read only for other users
- All file names in
The following is an example of output
bacman root -rwxr-xr-x
badblocks root -rwxr-xr-x
baobab root -rwxr-xr-x
base64 root -rwxr-xr-x
basename root -rwxr-xr-x
bash root -rwxr-xr-x
bashbug root -r-xr-xr-x
bayes.rb root -rwxr-xr-x
bbox root -rwxr-xr-x
s1.sh as follows:
#!/bin/bash
mkdir -p ./foo
myname="LiuYuhan"
myno='10132130104'
cd ./foo
echo $myname>name
#create file "name.txt" if it doesn't exit
echo $myno>stno
cp ./* ../
#the current directory is the foo directory
#!/bin/bash
find /bin -name 'b*' -type f|xargs ls -l >output.txt
#find the file named "b*" and list -l into output,if output.txt doesn't exit,then create it
awk '{print $9,$3,$1>"output.txt"}' output.txt
#that is the redirection in awk,remember that the name of file must wear ""
awk -F / '{print $3 >"output.txt" }' output.txt
sort output.txt -o output.txt
#sort it by asc order and redirect
1.Introduction
For s1.sh in part1,I use mkdir to create a directory foo in the current directory and use “echo” to write my name into a file named “name.txt”(if it doesn’t exit,it will be created automatically);the same with “stone.txt”.
For s2.sh in part 1,I use “find” to get the files named “b*” and “xargs” to execute “ls” from the input given by “find”,and then use “awk” to scan the lines and redirect the processed result into “input.txt”.
For part2,I use “gdb” to debug the given code and correct it.
2.usages
echo:display a line of text
xargs:build and execute lines from standard input
find:search for lines in a directory hierarchy
ls:list directory contents
awk:pattern scanning and text processing language
sort:sort lines of text files