Project 0a: Linux Warm Up
Intro
This project is to help U getting familiar with Linux and its operations.
Part 1 -1
way of executing s1.sh : %./s1.sh foo
The script first buildss a new directory foo in the current directory (the one contains s1.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
code:
#!/bin/bash
mkdir $1
touch name.txt
echo "吴彦祖" > name.txt
touch stno.txt
echo "PG12138" > stno.txt
cp name.txt stno.txt $1
before running this shell script, U need to do as following:
chmod 755 s1.sh
./s1.sh yourFileName
Part 1 -2
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. Furthermore, all file names in output should start with letter ‘b’ (other files should be excluded) - The file output is sorted by the file name field (in order of alphabet, from small to large) - The file output is read only for other users.
Here is an example 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
code:
#Date: 2020/09/15
#Author: Minxuan Li
#E-mail: 10185102208@stu.ecnu.edu.cn
#!/bin/bash
#create a file named 'output' first
touch output
#change its permission
chmod 644 output
#use pipe and regular expression to handle the output of command 'ls -l'
#name:$9 owner:$3 permission:$1
ls -l /bin | awk '{if($9~/^b+/) print $9,$3,$1}' > output
before running this shell script, U need to do as following:
chmod 755 s2.sh
./s1.sh
Part 2
gdb调试错误程序:set_operation.c
程序中一共出现了四处错误,分别是:
1)p->->next改为p->next ;
2)i<=A_size 改为 i<A_size ;
3)(p->next)->number 改为 p->number ;
4)去掉程序最后一行 system(“pause”) .
bonus 1
using awk to print the 10th line of a file. For example, assume that a file has the following content :
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
Your script should output the tenth line, which is :
Line 10
code:
#Date: 2020/09/25
#E-mail: 10185102208@stu.ecnu.edu.cn
#!/bin/bash
num_line=$(wc -l < $1)
#echo $num_line
awk ' {if($num_line>=10)
{if(NR==10)
{print;}
} }
END {if(NR<10) {print "there is no 10th line in your file";}}' $1
bonus 2
Given a text file, transpose its content. The file is in “column format”, which contains a bunch of rows, each row has a fixed number of columns, and the columns are separated by the ‘ ‘ character. For example, if a file has the following content :
name age
alice 21
ryan 30
Your script should output :
name alice ryan
age 21 30
code :
#Date: 2020/09/26
#E-mail: 10185102208@stu.ecnu.edu.cn
#!/bin/bash
awk '{ for(i=1;i<=NF;i++)
{ if(NR==1){ arr[i]=$i; }
else{ arr[i]=arr[i]" "$i; }
}
}
END{ for(i=1;i<=NF;i++){ print arr[i]; }
}' file.txt
Project 0b: Sorting
code :
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>
#include "sort.h"
#include <sys/types.h>
#include <sys/stat.h>
/* fastsort.c */
/* the rules of qsort */
int cmp(const void *a, const void *b){
rec_t a1, b1;
a1 = *(rec_t*)a;
b1 = *(rec_t*)b;
return a1.key - b1.key;
}
int main(int argc, char **argv)
{
/* the number of arguments in command line must be 3*/
if (argc != 3) {
fprintf(stderr, "Usage: fastsort inputfile outputfile\n");
exit(1);
}
/* open the two files (arguments in command line) */
char *inputFile = argv[1];
char *outputFile = argv[2];
/* get file descripter of the file that is called by system call */
int inputfd = open(inputFile, O_RDONLY);
int outputfd = open(outputFile, O_WRONLY|O_CREAT|O_TRUNC, S_IRWXU);
/* show the error information if the file can't be opened */
if (inputfd < 0) {
fprintf(stderr, "Error: Cannot open file %s\n", inputFile);
exit(1);
}
if (outputfd < 0) {
fprintf(stderr, "Error: Cannot open file %s\n", outputFile);
exit(1);
}
/* get the size of inputFile */
struct stat buf;
fstat(inputfd, &buf);
/* allocate space in the array */
rec_t* r = (rec_t*)malloc(sizeof(rec_t) * buf.st_size / 100);
/* read the data and store them in the array */
int cnt = 0;
while (1) {
int rc;
rc = read(inputfd, &r[cnt], sizeof(rec_t));
if (rc == 0)
break;
if (rc < 0) {
fprintf(stderr, "Error: Cannot read\n");
exit(1);
}
cnt++;
}
/* sort the array according to the key and write the result in outputfile */
qsort(r, cnt, sizeof(rec_t), cmp);
for (int i = 0; i < cnt; ++i) {
int rc = write(outputfd, &r[i], sizeof(rec_t));
if (rc != sizeof(rec_t)) {
fprintf(stderr, "Error: Cannot write\n");
exit(1);
}
}
/* need to close the files that were opened just now */
(void*)close(inputfd);
(void*)close(outputfd);
return 0;
}
/* 10185102208@stu.ecnu.edu.cn */
/* Date: Sep 27 2020 */