Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Answered Bash: How would i make this code into a script that uses a single file as input?

null_reflections

Legendary Coder
I found this series of commands online for copying a file multiple times...the way this is, all you have to do is put a real file's name in place of <input-file>, and then choose a name for the duplicates (it can be the same since the loop will suffix numbers to the name anyway...):

Code:
for i in {1..5}; do cp <input-file> "duplicate_name$i" ; done

Then, you will have 5 copies of the exact same file.

I'm wondering if there's a way to automate this as a script that takes the file as an argument? So lets say my script was named d5:

Code:
d5 <file-in-working-directory>

I know that scripts in bash will easily take arguments listed after them, but i'm having a lot of trouble figuring out how to make this work.
 
You can pass arguments to a bash script from cmd line using $ followed with a number. example

test.sh
Bash:
#! /bin/bash
echo "I am $1 and this $2"

output
Code:
./test.sh arg1 arg2
I am arg1 and this arg2
 
You can pass arguments to a bash script from cmd line using $ followed with a number. example

test.sh
Bash:
#! /bin/bash
echo "I am $1 and this $2"

output
Code:
./test.sh arg1 arg2
I am arg1 and this arg2
Yes, but that's not what i was asking about...the passing of arguments as variables in a shell script is designed to work like any command, where the arguments are inputs that get automatically plugged in to the script

I would like to know how i could adapt the above code i posted to command line arguments that could be used with any file that exists in your working directory.

I found out how to do the same thing, but it doesn't involve using a loop, i'd still like to know how to do that, but this script creates duplicates ending in a,b,c,d. There are four copies instead of five cuz i was feeling lazy earlier, lol:

Code:
#! /bin/bash
#creates four copies of an input file

cp $1 "$1a"

cp $1 "$1b"

cp $1 "$1c"

cp $1 "$1d"
 
I don't quite understand that copy command suggested by menator01 :

cp $file "$i""_""$file"

It's been a while since my last shell programming but I believe that may need to be :

cp $file "${i}_${file}"

But if the original script (or the version with my 'correction') creates no copies and there is no error message, add

set -x

as the first line (below the hashbang) to see how the commands are being evaluated.
 
Last edited by a moderator:
I try again. This will use a loop and make 5 copies naming is 1_input_file, 2_input_file and so on and so on.

Bash:
#! /bin/bash
for i in {1..5}
do
file=$1
cp $file "$i""_""$file"
done

Hope it helps.
Actually this works, i don't know what i was doing before, maybe i was just looking for a suffixed number with the file name (like with "ls <file>*" instead of "ls *<file>)...the number and "_" get prefixed to the filename.
 
Actually this works, i don't know what i was doing before, maybe i was just looking for a suffixed number with the file name (like with "ls <file>*" instead of "ls *<file>)...the number and "_" get prefixed to the filename.
ls -lt is your friend (list files most recent first). But what is the actual name of the created files ? I suspect the names may have a double quote in them which is probably not what you want ?
 
ls -lt is your friend (list files most recent first). But what is the actual name of the created files ? I suspect the names may have a double quote in them which is probably not what you want ?
glad you asked, just because i was about to erase the files:

Code:
3_copy-file
2_copy-file
1_copy-file
4_copy-file

I created "copy-file" with touch, which is just empty, and the script on my computer is named "d4". I need to start using naming conventions in file names, it's a lot better than creating an entire new directory for the scripts.
 
glad you asked, just because i was about to erase the files:

Code:
3_copy-file
2_copy-file
1_copy-file
4_copy-file

I created "copy-file" with touch, which is just empty, and the script on my computer is named "d4". I need to start using naming conventions in file names, it's a lot better than creating an entire new directory for the scripts.
I'm quite lost as to what is happening here now.... These file names are not a result of menator01's script, also not after my correction. How you created the input file is irrelevant, as is the name of your script. As for naming conventions for copied files, I'm not sure Linux has one, but you could adopt the Windows convention:

Code:
filename.ext
filename - Copy.ext
filename - Copy (2).ext
filename - Copy (3).ext
...

although maybe it's better, for bash, to leave out the spaces (and maybe the parentheses as well).

Anyway, is your issue solved now ?
 
I'm quite lost as to what is happening here now.... These file names are not a result of menator01's script, also not after my correction. How you created the input file is irrelevant, as is the name of your script. As for naming conventions for copied files, I'm not sure Linux has one, but you could adopt the Windows convention:
Yes, solved, and yes they are the result of menator01's script. Script as it looks on my computer:

Code:
file=$1
for i in {1..4}
do cp $file "$i""_""$file"
done

i'm going to use "copy-file" (an empty file) as target/argument for script. Only one copy exists currently:

Code:
ls *copy-file

copy-file

Now, for my script:

Code:
d4 copy-file

Now to verify it worked:

Code:
ls *copy-file

1_copy-file
2_copy-file
3_copy-file
4_copy-file
copy-file
 
Perhaps this version has a less troubling syntax:

Code:
file=$1
for i in {1..4}
do cp $file "$file""$i"
done

output:

Code:
copy-file
copy-file1
copy-file2
copy-file3
copy-file4

thanks again menator01!
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom