I am trying to write a script such that I can identify number of characters of the n-th largest file in a sub-directory. I was trying to assign n and the name of sub-directory into arguments like $1, $2.
Current directory: Greetings
Sub-directory: language_files, others
Sub-directory: English, German, French
Files: Goodmorning.csv, Goodafternoon.csv, Goodevening.csv ….
I would be at directory “Greetings”, while I indicating subdirectory (English, German, French), it would show the nth-largest file in the subdirectory indicated and calculate number of characters as well.
For instance, if I am trying to figure out number of characters of 2nd largest file in English, I did:
langs=$1
n=$2
for langs in language_files/;
Do count=$(find language_files/$1 name "*.csv" | wc -m | head -n -1 | sort -n -r | sed -n $2(p))
Done | echo "The file has $count bytes!"
The result I wanted was:
$ ./script1.sh English 2
The file has 1100 bytes!
The main problem of all the issue is the fact that I don't understand how variables and looping work in bash script.
foris notFor. To grab command output use command substitution. Even withfors!count_bytes=$(for langs in language/*; do echo $langs; done). bash is space sensitive -var='a'will work, butvar = 'a'will not! Usewc -cnotwc -m, i think it doesn't do what you think. bash uses"not“. And use dynamic programming - split your problem into many little problems.