Part 1
You need to write two shell scripts, both of them should be executable in a Linux shell.
-
s1.sh:% ./s1.sh fooThe script first builds a new directory
fooin 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.shoutput. Each row ofoutputrepresents 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
outputshould start with letter "b" (other file names should be excluded) - The file
outputis sorted by the file name field (in order of alphabet, from small to large) - File
outputis 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 redirect1.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
本文介绍两个实用的Shell脚本:s1.sh用于创建目录并写入文件;s2.sh用于生成包含特定文件名的文件列表,并按字母顺序排序。文章详细展示了如何使用常见Linux命令实现这些功能。
9197

被折叠的 条评论
为什么被折叠?



