在网上看到的不知源自何处的bash考试题目,抽出些许时间做了一下。

Part1 Write out the correct commands according to the questions. (20x1)
Examples:
command
function description command

terminate terminal session and log out : exit
current directory : .
parent directory : ..
Send output to stdout and file : tee -a file
-append if the file exist
display manual page for command : man command
start interactive communication with user1 : write user1
display string : echo string
Get input for command from a file : cmd < file
Questions:

1. Create an alias called ll that executes the 'ls –l' command.
ans:alas ll='ls -l'
2. Set a shell variable named VAR equal to the name “$(var)”. How do you see the contents of that variable?
ans:VAR='$(var)' or VAR="/$(var)"
3. Concatenate file1 and file2 into file3.
ans:cat file1 file2 >> file3
4. Remove the directory named is “Dir”, without any prompt information before removal.
ans:rm -rf Dir
5. How can you modify the permissions of the file “FILE”, so that your partners belonging to the same group can write the file ?
ans:chmod g+w FILE
6. Display the files start with character “a” and contain the character 'b'?
ans:find . -name 'a*b*' -print
7. Display the files start with “Foo” and end with three digits?
ans:$"find . -name 'Foo*[[:digit:]][[:digit:]][[:digit:]]' -print
8. Delete a user which name is “Foo” and his HOME directory must be removed at the same time.
ans:userdel -r Foo
9. Find the file less than 10k in the current directory.
ans:find -size -10k
10. Print the lines number of file “Foo.txt” on the screen.
ans:wc -l Foo.txt
11. Display all the current users in your computer:
ans:who
12. Assume a file contains more than 200 lines, display the lines from 101 to 150:
ans:sed -n -e '101,150p' file
13. List only sub-directory names (not include file names) in current directory:
ans:find -type d
14. Redirect standard error to standard out :
ans:2>&1
15. Mount an usb disk to the directory /disk/usb:
ans:mount /dev/sda1 /disk/usb
16. Switch to user named "user1":
ans:su user1
17. Create empty file or update timestamp on existing file:
ans:touch
18. Remove directories in interactive model:
ans:rmdir -i
19. Extract files from an archive test.tar.gz:
ans:tar -xvf test.tar.gz
20. Display files in long format including permissions, ownership and size:
ans:ls -l
Part2 Write out appropriate commands according to the questions. (10x2)
1. Print the lines of the file good.txt to the file bad.txt exclude the starting 10 lines:
ans:sed -n -e '11~1p' good.txt > bad.txt
2. Now there are two files named good.txt and bad.txt in the sub-directory test, how can you exchange the names of the two files?
ans:mv ./test/bad.txt good.txt && mv ./test/good.txt ./test/bad.txt && mv good.txt ./test
3. Mount Windows C disk (hda1) on the directory /winsys
ans:mount -t vfat /dev/hda1 /winsys
4. Remove all files and directories recursively from the current directory.
ans:rm -rf ./*
5. Print all the processes with the user id that are running on the system with the full information.
ans:ps aux | grep "^id"
6. How to sort the lines of a file named score according to the numeric sequence?
ans:sort -g score
7. In your HOME directory, create an empty file names goodnews
Change the permission of this file to -rwxr-x--x
ans:touch goodnews && chmod =751 goodnews
8. Check the disk usage of a directory named test, and only display the space size of the whole directory in byte.
ans:du -b --total ./test
9. Display yesterday’s date. Its output is in mm-dd-yy format.
ans:date -d "1 day ago" +%m-%d-%y
10. How can you print the lines which contain the string I'm $lost in the last 100 lines of the file named badnews?
ans:tac badnews | sed -n -e '1,100/p' | sed -n -e "/I'm /$lost/p"
Part 3: (8+8+10+14)
1. Write a shell script to check the items of a designated directory which must be input from your terminal: if the item is a directory, delete it; else print the name of this item.
ans:
#!/bin/bash
if [ -d $1 ];then
    rm -rf $1
else
    basename $1
fi
2. Write a shell script to display the odd lines of a given file.
ans:
#!/bin/bash
if [ -f $1 ];then
    sed -n -e '1~2p' $1
fi
3. Write a shell script using a directory as input, if the input is null, use current directory as input. The routine is used to find all files with suffix ".c" and ".cpp", and then compare each other to check out whether there exist two files having the same contents: if exist, print out the corresponding names of these files.
ans:
#!/bin/bash

if [ "x$1" == "x" ];then
    dir=`pwd`
else
    dir=$1
fi
index=0
for fn in `find $dir -type f -name '*.c' -print`;do
    array[$index]=$fn
    index=$((index+1))
done
for fn in `find $dir -type f -name '*.cpp' -print`;do
    array[$index]=$fn
    index=$((index+1))
done
n=${#array[@]}
for((i = 0; i < $n; i++));do
    for((j = i + 1; j < $n; j++));do
        cmp ${array[$i]} ${array[$j]} > /dev/null && echo "${array[$i]} and ${array[$j]} is same!"
    done
done
4. Write an ANSI C code. Use a Makefile to build it (including compile and link) into binary file.
The function of this binary file should be:
Read a serial of numbers from a given file, and then find the Top 5 maximum numbers.
You should give:
1. Makefile ( hint : one main file and one sub-routine file )
ans:
default: findmax.o main.o
    cc findmax.o main.o -o project
findmax.o: findmax.c
    cc -c findmax.c
main.o: main.c myhead.h
    cc -c main.c
clean:
    rm *.o
2. all C source code
ans:
findmax.c:
int findmax(int data[], int start, int end)
{
    int max = data[start];
    int i, index, t;

    index = start;
    for (i = start + 1; i < end; i++){
        if (max < data[i]){
            max = data[i];
            index = i;
        }
    }
    if (index > start){
        t = data[start];
        data[start] = data[index];
        data[index] = t;
    }
    return data[start];
}
main.c:
#include "myhead.h"

int main(int argc, char ** argv)
{
    FILE * fp;
    int data[MAX_NUM];
    int count = 0;
    int i;

    fp = fopen(argv[1], "r");
    if (fp < 0){
        printf("Cannot open file %s/n", argv[1]);
        return -1;
    }
    while(fscanf(fp, "%d", &data[count]) != EOF) count++;
    for (i = 0; i < 5 && i < count; i++){
        printf("%d/n", findmax(data, i, count));
    }
    fclose(fp);
    return 0;
}

3. the whole procedure of how you compile and link
ans:
cc -c findmax.c
cc -c main.c myhead.h
cc findmax.o main.o -o project


NOTE:this problem is essential for linux programming, so you'd better do it all by yourself.
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值