Reputation: 5646
i am new to shell scripting and i found following line of code in a given script.
could someone explain me what does the following lines of codes means
_filecount=echo ${_filelist} | wc -w | awk '{ print $1 }'
printk "archiveWLS(): ${_filecount} file(s)"
Thanks in advance for any help
Upvotes: 0
Views: 7914
Reputation: 9025
It stores the number of words in _filelist variable into _filecount. Elaboration :
_filecount=
`echo ${_filelist} | wc -w | awk '{ print $1 }'
`
you have to wrap it under `s,for this is the rule.
echo gives out what's stored in _filelist to wc.
wc (word counter ) receives it (this passing of intermediate result is called piping),and because it's invoked with a -w option, it already gives out only the number of words to awk,and keeps the count of lines and characters to itself.
awk in turn prints out first column of what's supplied to it ( awk '{ print $1 }'`).
wc only supplies 1 column here,i.e. the word count,so calling awk is redundant actually.
printf "archiveWLS(): ${_filecount} file(s)"
substitutes the value of _filecount into the string and gives it out to the standard output stream,which happens to be your shell output window. That's it.
Upvotes: 1
Reputation: 26106
What this script does is, essentially, nothing.
_filecount=echo ${_filelist} | wc -w | awk '{ print $1 }'
This line is syntactically valid but not likely to be correct.
Because echo
appears where it does it is being interpreted as a temporary variable assignment that exists only for the duration of the following command, Thus, the environment variable _filecount
will have a value of echo
during the run of the command stored in the variable _filelist
; since, I imagine, _filelist
doesn't contain a command name and, instead, contains the name of one or more non-executable files, you will get a command not found
error.
The rest of the line is irrelevant. Since the ${_filelist}
command output nothing to stdout, nothing was redirected to wc -w
. wc
will report that 0 words werre counted and this value will be passed to awk
, which will strip the leading whitespace.
What you probably meant to say was more like
_filecount=$(echo ${_filelist} | wc -w | awk '{ print $1 }')
Which will execute the echo
pipeline and store the result in the _filecount
variable.
It isn't really a good idea to store a file list in a space-delimited string like this (for one thing, files can have spaces in their names), instead use a bash
array, like this:
_filelist=("file1.txt" "file2.txt")
You can expand this to the list of files with "${_filelist[@]}"
, all properly quoted, and you can now obtain the count of files this way:
_filecount=${#_filelist[@]}
Which is much faster (executes in-process instead of calling external utilities) and more reliable (doesn't rely on "one file is one word" semantics).
As for this line:
printk "archiveWLS(): ${_filecount} file(s)"
Since there is no bash builtin called printk
and there is no standard utility called printk
, the result is likely to be:
-bash: printk: command not found
You could have used printf
for this:
printf "archiveWLS(): ${_filecount} file(s)"
And the output would be
archiveWLS(): 0 file(s)
Where 0
is the value of _filecount
.
Although, it would be nicer to say:
printf 'archiveWLS(): %s file(s)\n' "${_filecount}"
Which will output the same as the above, also include a newline at the end and be easier to maintain.
Upvotes: 0
Reputation: 36269
_filecount=echo ${_filelist} | wc -w | awk '{ print $1 }'
wc -w
: wc is word-count, wordcount counts words, lines, characters and so on, -w only outputs the word count.
The output is piped to awk, to print only the first word of output. If you use
wc -w FILE
the filename would be printed, and it would make sense to strip it away with awk. Doing it with a piped output, where there is no filename, is superflous.
As assignment, the line should instead be:
_filecount=$(echo ${_filelist} | wc -w )
Without $(...) or backticks, it doesn't work, and filecount is just assigned the word "echo". Seems like a poorly written script.
printk isn't defined in bash, nor is it a well established gnu programm from the coreutils.
Maybe it is defined in the script itself.
Upvotes: 3