Write a shell script program that contains a function called
Write a shell script program that contains a function called findFiles(). This function can list all files of a particular type in the current directory and then copy them to a new director called MyFiles. The file type (such as shell script files with a appendix .sh ) will be passed as a parameter when the function is called. You program also needs a function call to the findFiles() function to test it. For example:
findFiles sh
Note: sh is the parameter to be passed into findFiles(). Then the output of your program should be all shell script files with the appendix .sh and all of these files will be copied into a new directory called MyFiles.
Hints: you can use built-in linux commands in your shell scripts such as ls, mkdir...
Solution
#!/bin/sh
# Define your function here
FindFiles () {
mkdir $1
ls |grep `echo $2`|while read c
do
cp ./`echo $c` ./$1/
done
}
echo \"Enter your new dir\"
read a
echo \"Enter your file type\"
read b
# Invoke your function
Findfiles `echo $a $b`
