The first part of what i am mucking around with simply creates a folder entitled command_manuals that contain text file copies of all the commands available at the bash shell, the filenames which are the command names themselves.Aside from some unusual error messages that do not affect the script from achieving it's goal, this part I have managed to get, but I would prefer to go to the most crippling issues first so I will leave it out for now.
Those without manuals available have empty text files still, which brings me the goal of the second part, which is to delete these "0 size text files" and compile a single list for which I want to use in a later part, that checks the "undocumented man 7" and other parts every time i execute apt-update and uses one of the well known pattern searching packages to see if anything new has been added regarding those commands missing manuals, ra,ra,ra anyway not really important until i get over this drama of part two, which thus far i have successfully written an if condition into the for loop that simply prints out the subset of the folder's contents with a size of zero, but the trouble starts when i try to replay this basic echo command between "then" and "fi" that deletes the empty files.
But this also brings me to an additional question concerning another odd thing i noticed, the basic template script that works as i desired, is missing the standard #!/bin/bash that is always required as the first line for .sh scripts in my previous experience, why?
template_for_if_delete.sh
TOTALnum=$(wc -l < filesizes.txt);
for i in `seq 1 $TOTALnum`;
do
size=$(sed -n ''$i','$i'p' filesizes.txt)
if [ $size -eq 0 ];
then
echo $(sed -n ''$i','$i'p' filenames.txt);
fi
done;
So far for the actual script for the second task i have:
secondpart.sh
#!/bin/bash
cd;
cd command_manuals;
rm filenames.txt;
ls -1 > filenames.txt;
TOTALnum=$(wc -l < filenames.txt);
for i in `seq 1 $TOTALnum`;
do filename=echo $(sed -n ''$i','$i'p' filenames.txt);
size=$(stat -c '%s' $filename);
if [ $size -eq 0 ];
then
rm $(locate $(sed -n ''$i','$i'p' filenames.txt));
fi
done;
which produces the output (in repetition a number of times as per the quantity defined in the for loop above):
Try 'stat --help' for more information.
./secondpart.sh: line 9: [: -eq: unary operator expected
./secondpart.sh: line 8: x86_64-linux-gnu-gold.txt: command not found
stat: missing operand
I then tried to change to complex parameter expansion for the variable 'size' in the if statement:
#!/bin/bash
cd;
cd command_manuals;
rm filenames.txt;
ls -1 > filenames.txt;
TOTALnum=$(wc -l < filenames.txt);
for i in `seq 1 $TOTALnum`;
do filename=echo $(sed -n ''$i','$i'p' filenames.txt);
size=$(stat -c '%s' $filename);
if [ ${#size} -eq 0 ];
then
rm $(locate $(sed -n ''$i','$i'p' filenames.txt));
fi
done;
which produced the concerning output asking me if i want to delete some random unrelated stuff, at which point i decided ok i better post a question before i mess up my virtual machine thats been running so well for me:
./secondpart.sh: line 8: filenames.txt: command not found
stat: missing operand
Try 'stat --help' for more information.
rm: missing operand
Try 'rm --help' for more information.
./secondpart.sh: line 8: file-roller.txt: command not found
stat: missing operand
Try 'stat --help' for more information.
rm: missing operand
Try 'rm --help' for more information./code>
./secondpart.sh: line 8: filesizes.txt: command not found
stat: missing operand
Try 'stat --help' for more information.
rm: missing operand
Try 'rm --help' for more information.
./secondpart.sh: line 8: file.txt: command not found
stat: missing operand
Try 'stat --help' for more information.
rm: cannot remove adammvirtual/Win10UbuntuVM001_apt_sources_file.txt':
No such file or directory
rm: cannot remove '/home/adamvirtual/mapfile.txt': No such file or directory
rm: remove write-protected regular file '/usr/share/doc/alsa-base/driver/Procfile.txt.gz'?
I pressed ctrl+C to escape the prompt.
man
command itself. Start withman less
(or this abbreviated guide) to learn just to navigate a single man page, and then readman man
.man -w
in particular might be useful to you; there are a lot of other goodies. You don't have to reinvent the wheel. – Wildcard May 22 '19 at 05:12