Is it a bad practice to write shell script with many if-else statment and for loops?

I am maintaing several data process shell scripts which are full of if-else statements and for loops . I try to make the scritps tidy and easy to debug.

When I read some suggestions about shell code best practice, it seems that is is not a best practice to have many if-else and for loop statements.

So what should I do, to write a python or c++ script instead of those logical process?

1

It’s never a good idea to have too many embedded if/else or loops, but in their own right, lots aren’t inherently a bad thing.

Consider splitting out the loops unto utility functions. For example, if you have:

for stuff ..
   for stuff ...

   endfor
endfor

It would be much better read as

findAllPricesForProducts( extractNamePriceTuple( data ) )

with the utility functions defined appropriately. If you then document each function and give an example input/output, your code should be both readable and maintainable.

As a perl coder (this could easily be adjusted to python) I have heard said and said myself “any shell script longer than 10 lines should be a perl script.” Shell scripts above a certain threshold in size/complexity often become nightmarish to maintain. Switching to a preferred scripting language can drastically simplify this (and get away from using repeated calls grep, awk, and sed (or other lesser known applications).

Language choice aside, another comment I have heard is “if the function requires the word ‘and’ to describe its functionality, it is too big/complex.” It isn’t a problem to have nested statements, but if the logic around the statements becomes complex one should consider to extract parts of it so that it is something one can keep in their head while debugging and be able to simply test the functionality of it.

There’s no issue with using if- and for-statements in a shell script. Is your code deeply nested in many if/for-statements?

Don’t forget that shell languages have user-defined functions should you with to make your code more modular for maintainability.

The main argument against writing complex shell scripts is that they are difficult to debug and easy to break. That said, simply taking the same methodology into another language wouldn’t be an improvement – it’s pretty much inherent if what you are doing is stitching together a bunch of other programs.

If you should rewrite the scripts in a language like python depends much on

  • how many scripts with how many lines of code you have

  • how many new requirements you expect to be added to them in the nearby future (if there are none, why bother?)

  • how messy they really are

  • how sure you are that you can implement them really better without introducing new bugs

And before doing any rewrite, even it is a small one like it seems to be in your case, please read this famous article of Joel Spolsky why a rewrite is often a bad choice.

1

As in any language…

  • Try never to write the same logic more than once
    • Maybe a script with many if-then statements can be reduced to a few functions.
  • Loops (do, for, while, repeat, until), by their nature, are semi-self-contained engines. They’re analogous to electric motors in a complex machine. Errors in them are almost universally the cause of all “freeze”, “lockup”, “100% CPU” and similar catastrophic bugs.
    • Use sparingly, I would suggest never to have multiple similar loops. “Genericize” the code so the same loop can be put in a function and reused with different variables and conditions. (from one who has taken days to see an ‘l’ (el) variable that should have been an ‘i’ (eye))

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật