兩題 Linux shell 語法 問題

2題 Linux Shell 語法 的問題, 請高手幫忙我解答, 拜託, 謝謝

1. Write a short script that tells you whether the permissions for two files, whose names are given as arguments to the script, are identical. If the permissions for the two files are identical, output the common permission field. Otherwise, output each filename followed by its permission field. (Hint: Try using the cut utility.)

2. Write a script that takes a colon-separated list of items and outputs the items, one per line, to standard output (without the colons).

1 Answer

Rating
  • 1 decade ago
    Favorite Answer

    第1題

    #!/bin/sh

    if [ -e tmp ]; then

    echo "there is a dir called tmp!" > /dev/null

    else

    echo "create a tmp dir"

    mkdir tmp > /dev/null

    fi

    ls -l $1 | cut -d' ' -f1-4 > tmp/r1.txt

    ls -l $2 | cut -d' ' -f1-4 > tmp/r2.txt

    if diff tmp/r1.txt tmp/r2.txt > /dev/null

    then

    echo "The permisssions of two files are the same"

    cat tmp/r1.txt

    else

    echo "The permissions of two files are different"

    echo $1":"

    cat tmp/r1.txt

    echo $2":"

    cat tmp/r2.txt

    fi

    rm -rf tmp/r1.txt

    rm -rf tmp/r2.txt

    第2題

    我把 colon-separated list of items 視為參數輸入1

    #!/bin/sh

    echo "the origin is " $1

    echo "the items without the colons are "

    echo $1 |tr -s ':' '\n'

    不視為參數而改為預設值的話

    #!/bin/sh

    list=abcd:23f:35fv

    echo "the origin is " $list

    echo "the items without the colons are "

    echo $1ist |tr -s ':' '\n'

Still have questions? Get your answers by asking now.