Project0(Get familiar with Linux) (Part 1)

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 contains sh1.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 of foo.

  • s2.sh:

    % ./s2.sh
    It generates a text file output. Each row of output 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

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       

s2.sh as follows:
#!/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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值