Home > other >  How to create a script that is automated and sequential
How to create a script that is automated and sequential

Time:01-20

I'm struggling in trying to create a script in Linux (Terminal-Bash) which is automated and when run it will ask for a input and when that input is added it will create sequential files.

See below: when script is run, it should show

Which country are you from? I enter Brazil and wish to see:

Data entered: Brazil1, Brazil2, Brazil3.....Brazil 10

The script needs create a batch of 10 each time it is run i.e.

Which country are you from? I enter Canada and wish to see:

Canada1, Canada2, Canada3.....Canada10

If brazil entered again: Brazil11, Brazil12....Brazil20 and Brazil21, Brazil22....Brazil30 etc

I do not want to hardcode the numbers, automation is required to create them each time.

**Testing: ** I have created the script by:

Touch test.sh

I have tried to edit the script by:

vim test.sh

In vim I have made the below changes:

#!bin/bash

echo "Which country are you from"

value=country

value{1..10}

Save vim, when executing script I get this message:

test.sh: line 4: value1: command not found

Can someone please help with the script? I'm completely new to linux and trying to best understand how to create the simplest and most effective process.

Thanks in advance.

CodePudding user response:

This script is pretty straightforward to implement. You need to use the read command to read user input in. You can use the while loop with -f to test which files already exist, and then finally the for loop to create those files

#!/bin/bash

echo Hello, what country are you from?

read user_country

n=1

while [ -f "$user_country$n" ]
do
    let "n =10"
done

for ((i=0; i<10; i  ))
do
    file_num=$((i   n))
    touch "$user_country$file_num"
done
  • Related