AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Ubuntu batch script example4/11/2024 ![]() Imagine that you have a file that you want to scp to several servers. In a directory with hundreds of files, this loop saves you a considerable amount of time in renaming all of them. The files will be renamed like this: file1_20210210.pdf To illustrate, run this loop in a directory containing these files: file1.pdf In this case, we're adding today's date to the end of the file name (but before the file extension). This loop takes the output of the Bash command ls *.pdf and performs an action on each returned file name. ![]() $ for name in joey suzy bobby do echo first $name echo second $name done New commands just need a delimiting them. Anything between do and done will be executed. You can also perform more than one action per loop. The variable $name will contain the item in the list that the loop is currently operating on, and once the command (or commands) in the do section are carried out, the loop will move to the next item. That's about as simple as it gets and there isn't a whole lot going on there, but it gets you started. Here's an example: $ for name in joey suzy bobby do echo $name done The list of items can be anything that returns a space or newline-separated list. The variable name will be the variable you specify in the do section and will contain the item in the loop that you're on. The basic syntax of a for loop is: for in do $ done Basic structure of the for loopįirst, let's talk about the basic structure of a for loop, and then we'll get into some examples. In this article, we show you some examples of how a for loop can make you look like a command line hero, and then we take some of those examples and put them inside a more structured Bash script. Converting this loop structure into a Bash script is also trivial. For example, text pulled from a file, the output of another Bash command, or parameters passed via the command line. The given set of items can be a literal set of objects or anything that Bash can extrapolate to a list. Automate Red Hat Enterprise Linux with Ansible and Satellite.Ansible Automation Platform trial subscription.A system administrator's guide to IT automation.Ansible Automation Platform beginner's guide.The basic concept is: FOR a given set of items, DO a thing. ![]() If you're not, I'll try to break it down in plain English for you. If you already have a programming or scripting background, you're probably familiar with what for loops do. However, if you're trying to rename a thousand files, or execute the same command several times, then the for loop is definitely the right tool for the job. If you want to automate the configuration of thousands of systems, you should probably use Ansible. Most of them do not compare to the simplicity and ready usability of the for loop, though. There are other approaches to automation that are certainly more robust or scalable. For many of us, that thing is looping in Bash. Every sysadmin probably has some skill they've learned over the years that they can point at and say, "That changed my world." That skill, or that bit of information, or that technique just changed how I do things. ![]()
0 Comments
Read More
Leave a Reply. |