Bash Challenge
At the end of the Linux module AWS set a challenge to create a small BASH script to create files. This is the outline the specified in the exercise document
Write a Bash script based on the following requirements:
- Creates 25 empty (0 KB) files. (Hint: Use the touch command.)
- The file names should be <yourName><number>, <yourName><number+1>, <yourName><number+2>, and so on.
- Design the script so that each time you run it, it creates the next batch of 25 files with increasing numbers starting with the last or maximum number that already exists.
- Do not hard code these numbers. You need to generate them by using automation.
Test the script. Display a long list of the directory and its contents to validate that the script created the expected files.
Lets start, lets start with the information in the bullet points. We have to create files with my name Paul and with an incrementing number starting with the highest number. So if the last file was paul105.csv then the next time the script is run it should start with 106.csv and create 25 more.
What I know so far with planning. Firstly, I need to create a variable called empName to store the name used in the touch command. Second the touch command to create the files with a .csv extension. Lastly I have a specific number of files to create, 25. I can use a loop for this functionality. As I know it needs to run 25 times it will be a for loop.
Okay lets start the script. First start nano with the name of the script. I called mine filecreator.sh.
nano filecreator.sh
Once its opened, we need to add #!/bin/bash. First lets set a variable then use the touch command to make the file. After that we can save and test so far. The script looks like this so far.
#!/bin/bash
empName="Paul"
touch $empName.csv
Oops did not expect that to happen. Lets fix this error and then try to rerun it. To fix the err I changed the permissions by adding execute permission on fileCreator.sh. The command was chmod +x flieCreator.sh. Now when we run the script again. This time we need also run ls command to see the file it created.
Leave a Reply