Home > Software engineering >  Creating as many files as the number of arguments to bash script
Creating as many files as the number of arguments to bash script

Time:12-05

Two arguments will be entered. One of these arguments will be the name of the file and the other will be the number to be generated.

$./Project1.sh ./One.dic 4

-- Created: One-1.dic One-2.dic One-3.dic One-4.dic

#!/bin/bash

echo "name": $1";
echo "number: $2";

for ((i=1;i<=number;i=i 1))
do 
  touch ???
done

CodePudding user response:

Assumptions:

  • extension of the name (.dic in this case) is not known in advance
  • the name will always include an extension
  • if the name has multiple extensions (eg, .dic.orig) we want to place the number before the last extension (eg, .dic-1.orig)
  • while OP's expected output shows the (relative) path removed (./ in this case), I'm going to assume we do not want to remove the relative path (eg, what if name is new/sub/dir/One.dic?)
  • we won't worry about creating subdirectories that show up in the name (eg, if name=new/sub/dir/One.dic we'll assume the directories new/sub/dir already exist)

One idea:

name="$1"
number="$2"

# use parameter expansion to break name into 2 parts

base="${name%.*}"
ext="${name##*.}"

for ((i=1;i<=number;i  ))
do
    newname="${base}-${i}.${ext}"
    echo "${newname}"
done

For name=./One.dic and number=4 this generates:

./One-1.dic
./One-2.dic
./One-3.dic
./One-4.dic

For name=new/sub/dir/One.dic.orig and number=3 this generates:

new/sub/dir/One.dic-1.orig
new/sub/dir/One.dic-2.orig
new/sub/dir/One.dic-3.orig

NOTES:

  • once the results are verified OP can replace echo with touch (or whatever command is desired to create/populate the new file)
  • OP may want to add some logic to verify the input parameters; for number it must be an integer; for name it must contain an extension ... this would entail more than just testing for a period since ./One has a period but no extension
  • OP will need to decide how to handle name if (when?) it contains path/directories (eg, for name=new/sub/dir/One.dic does OP want to abort if directories do not exist? or go ahead and create the subdirectories?)

CodePudding user response:

#To create multiple files using a bash script, you can use a loop and the touch command. The touch command is used to create an empty file with a specified name. Here is an example of a script that creates as many files as the number of arguments passed to it:

#!/bin/bash

# Get the name of the file from the first argument
name=$1

# Get the number of files to create from the second argument
number=$2

# Use a loop to create the specified number of files
for ((i=1;i<=number;i=i 1))
do 
  # Create a file with the specified name and an index number appended to it
  touch "${name}-${i}.dic"
done

#In the example above, the script takes two arguments: the name of the file and the number of files to create. It then uses a loop to create the specified number of files, using the touch command and appending an index number to the file name.

#To use the script, you would call it from the command line and pass the name of the file and the number of files to create as arguments, like this:

$./Project1.sh ./One.dic 4

#This would create four files named One-1.dic, One-2.dic, One-3.dic, and One-4.dic.

CodePudding user response:

#You can try it then 
#To create a script that creates a number of files, you could use the following code:

#!/bin/bash

# Check if at least one argument was provided
if [ $# -eq 0 ]
then
    echo "Please provide the number of files to create as an argument."
    exit 1
fi

# Create the specified number of files
for ((i=1; i<=$1; i  ))
do
    touch "file$i"
done

#To use the script, save it to a file with a .sh extension and make it executable using the chmod command:

chmod  x create_files.sh

#You can then run the script and specify the number of files to create as an argument:

./create_files.sh 5

#This will create five files named file1, file2, file3, file4, and file5.
  • Related