| |

VerySource

 Forgot password?
 Register
Search
View: 1297|Reply: 7

Help me see this program title

[Copy link]

5

Threads

14

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-2-19 12:00:02
| Show all posts |Read mode
Write a shell script that takes a folder path as input, and if the input is empty, the current directory is used as input. Print out the file names in this directory with the suffixes .c and .cpp, and then compare the files. If there are files with the same content, print their names!
Reply

Use magic Report

0

Threads

10

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 Invalid IP Address

Post time: 2020-4-29 03:00:01
| Show all posts
Not complicated, you should study it yourself
Problems involved
1. The script receives command line arguments
2. Determine the current directory
3. Traverse a certain type of file
4. Compare file contents
5. Looping of script programs
Reply

Use magic Report

5

Threads

14

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-5-5 19:15:01
| Show all posts
I just do n’t know how to do it in the loop,
Reply

Use magic Report

5

Threads

14

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 Brazil

 Author| Post time: 2020-7-22 11:45:01
| Show all posts
#! /bin/sh
function compare
{
if [`cd $Dname` ]; then
            list=`ls *.{.c,.cpp}`
        echo "the result of compare:"
        for file in $list; do
             for otherfile in $list; do
                   if [$file!=$otherfile ]; then
                        if [![ cmp $file $otherfile] ]; then
                                echo "$file and $otherfile have the same content"
                        fi
                   fi
             done
         done
}
                          
echo "input a directory name"
read Dname
if [$Dname!="" ]; then
compare
else
$Dname="." #use current directory as input
compare
fi

#Is there something wrong with my writing? I have adjusted it and I haven't debugged it now. Which expert can help me see! Thank you
Reply

Use magic Report

5

Threads

14

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-7-22 12:00:01
| Show all posts
A "fi" is missing in the function
Reply

Use magic Report

0

Threads

10

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 Invalid IP Address

Post time: 2020-7-22 17:15:01
| Show all posts
if [`cd $Dname` ]; then
list=`ls *.{.c,.cpp}`
fi
--------------------------
In addition, your main program part can be adjusted and optimized
echo "input a directory name"
read Dname
if [$Dname="" ]; then
$Dname="."
fi
compare
Reply

Use magic Report

0

Threads

10

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 Invalid IP Address

Post time: 2020-7-22 19:15:01
| Show all posts
In fact, the main program may be safer like this
echo "input a directory name"
read Dname
if [-n $Dname ]; then
   if [-d $Dname ]; then
     echo'The input parameter is not a valid directory'
     exit 1
   fi
else
     $Dname="."
fi
compare
------------------------------------------
Generally, the input of directory parameters is best to use command line parameters, so that it is more convenient for programs to call each other.
In the result output part defined by function compare, it is best to change echo "the result of compare:" to
echo "the result of compare in"`pwd`":"
Reply

Use magic Report

5

Threads

14

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

 Author| Post time: 2020-7-30 08:45:01
| Show all posts
thank you very much! !
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list