2
\$\begingroup\$

I wrote a short bash script for a completion check of downloading images.

goal of this code (Added)

I have some downloading content directories. Each of them has a script for downloading images(tmp.sh), for example, the following one:

wget -nc \
 http://www.example.com/001.jpg \
 http://www.example.com/002.jpg \
 ...
 http://www.example.com/123.jpg \

I want to know which wget completes downloading all or failed.

If you have any good ideas, please tell me them. Thank you.

done.sh

#!/bin/sh
# if a directory name is assigned
if [ $# -eq 1 ]; then
 a=`ls -1 1ドル/*.jpg | wc -l`
 b=`grep jpg 1ドル/tmp.sh | wc -l`
 if [ $a = $b ]; then
 echo 1ドル
 fi
 exit
fi
# if some directory names are assigned
if [ $# -gt 1 ]; then
 for dir1 in "$@"; do
 ./done.sh $dir1
 done
fi
# if directory names are not assigned
if [ $# -eq 0 ]; then
 ls -d */ | xargs ./done.sh
fi
```
asked May 30, 2021 at 0:33
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Good

  • good indentation
  • somewhat helpful comments
  • good to do the ; then bit on the same line as the if

Suggestions

  • use double brackets for conditionals
  • try shellcheck (It will tell you to put quotes around your variable substitutions.)
  • put your arguments into named variables so you don't keep have to referring to 1ドル
  • put the 3 sections into if...elif...else structure to make clear that you're only going to go into one of them
  • check for the existance of ./done.sh at the beginning
  • explain what the goal of the script is somewhere.
answered May 30, 2021 at 3:04
\$\endgroup\$
3
  • \$\begingroup\$ Thanks for the great suggestions. I forgot the programming fundamental techniques in shell programming. I'll correct my code and paste it below the original code. \$\endgroup\$ Commented May 30, 2021 at 4:04
  • \$\begingroup\$ @HaruoWakakusa You may want to read What I can and cannot do after receiving answers before you do that. As it is one of the things that you can't do under this site's rules. \$\endgroup\$ Commented May 30, 2021 at 4:10
  • 1
    \$\begingroup\$ Yes, I'll add only the purpose of this code. \$\endgroup\$ Commented May 30, 2021 at 4:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.