First, I am newer than new, I'm sorry this is so long, but the forum rules ask for details.
Also sorry this is printing out weird.
Here is what I am trying to do: I am stuck on part (B)
The script you create must use only relative paths for all paths in the code. You must assume that the script is being run from the ~ directory.
(A) Create a shell script that loops through all items in a directory given by the user using the "read" builtin. The script must validate the input and use a loop to only allow the script to proceed after the validation is complete. It must validate that the directory given is a directory.
(B) The script, while iterating over the items in the directory, should test the items to see if they are directories or files.
(B.1) If they are directories then the script needs to loop over the directory and count the number of items inside of the directory (not recursively). The script needs to output both the name of the directory and the number of files (and folders) inside of it to the same line of a file named "~/sorter/directory_listing.txt".
(B.2) If they are files they need to be examined for size using du with human readable output. Your script will have to write the size followed by a space then the file name to a new line of the file "~/sorter/files_and_sizes.txt".
(C) After the loop finishes the files_and_sizes.txt file needs to be sorted human readable then output to a file ~/sorter/files_sorted.txt The directory_listing.txt file needs to be sorted reverse by the number of items in the directory and output to a file named ~/sorter/dir_reverse.txt
My attempt:
read -p "Please enter a directory " chosen_dir
while [[ ! -d "$chosen_dir" ]]
do
echo Please enter a valid directory && read chosen_dir
done
for i in $chosen_dir/*;
do
counter=$((counter+1))
if [ -d "$chosen_dir" ]; then
ls "$chosen_dir" | wc -l > sorter/directory_listing.txt
else
if [ -f "$chosen_dir" ]; then
du -sh "$chosen_dir"/* > sorter/files_and_sizes.txt
fi
done
RESULT:
blahblah@vm1:~$ ./taratest.sh
Please enter a directory testsort
./taratest.sh: line 26: syntax error near unexpected token `done'
./taratest.sh: line 26: ` done'
When I remove the done
I get this ….
NEW RESULT:
blahblah@vm1:~$ ./taratest.sh
Please enter a directory testsort
./taratest.sh: line 27: syntax error: unexpected end of file
THEN: I searched on here for while iterating over the directory, test to see if the items are files or directories in bash
and changed to THIS:
first part of script, then…
for i in "$chosen_dir"/*
do
[ -f "$chosen_dir" ] && echo "is File"
[ -d "$chosen_dir" ] && echo "is Directory"
done
I KNOW THE OUTPUT IS NOT WHAT'S IN THE DIRECTIONS, I AM ECHOING JUST FOR TEST PURPOSES
RESULTS:
Please enter a directory testsort
is Directory
is Directory
is Directory
is Directory
2 are files & 2 are dirs., so why is it saying 4 dirs
blahblah@vm1:~$ ls -lh testsort
total 8.0K
drwxrwxrwx 2 blahblah student 68 Jun 4 17:41 dir1
drwxrwxrwx 2 blahblah student 68 Jun 4 17:41 dir2
-rwxrwxrwx 1 blahblah student 0 Jun 4 17:41 file1.txt
-rwxrwxrwx 1 blahblah student 0 Jun 4 17:41 file2.txt
Any guidance on solutions is greatly appreciated.
for i in "$chosen_dir"/*
so your loop variable is$i
i.e. you should be doing[ -f "$i" ]
and so on. As it stands, you are repeatedly testing the parent directory"$chosen_dir"
instead. – steeldriver Jun 06 '16 at 00:18