From the course: Learning Linux Shell Scripting

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: Pipes and functions

Solution: Pipes and functions - Linux Tutorial

From the course: Learning Linux Shell Scripting

Start my 1-month free trial

Solution: Pipes and functions

(whoosh, chime) - [Instructor] How did you do at this challenge? Hopefully you solved it on your own. Either way, here is my version of a solution. So we're gonna say touch pfunc.sh, do a change mode 755 pfunc.sh, and finally atom pfunc.sh. Go ahead and add our shebang. And then we'll add our first function, so this is gonna be function get files. There is no easy way to return a value from a function, so we will use a global variable, files, to hold the file names, so files and it's gonna be equal to, and note these are back ticks, ls dash one, and we have a pipe, this time we're gonna use a sort with no options, so it's just gonna do an alphabetical sort, then we have another pipe, then we're gonna do a head dash 10, so that will take the first 10 files. Then below that, we're gonna do a function show files. Open and close parentheses, open and close curly braces, in here we'll use a local variable, local count, which is gonna be initialized to one, then we're gonna do a for loop…

Contents