I'm trying to write a program that accept file names as arguments in a bash script, then passes them to a C program that replaces spaces with underscores, then the bash script uses that to rename the file.
For example, the input would be
Bash bash_script "test test test.txt"
and the file would be renamed test_test_test.txt.
My problem is that when I run this, it tells me that I'm using mv incorrectly. Why is this? I'm new to bash, so I'm sorry for using program/script incorrectly.
My C program:
#include <stdio.h>
int main(int argc, char *argv[])
{
int i = 0;
char * file = argv[1];
while(i<=256){ //max file size is 256 characters on mac
if (argc != 2)
printf("Please provide one file name.");
else if(file[i] == ' ')
file[i] = '_';
i++;
}
printf("%s \n", file);
return 0;
}
My Bash program:
#! /bin/bash
VAR = "C_program '$@'"
mv $1 $VAR
trin your shell script (ref: linux.die.net/man/1/tr )abc def" but there is already a file "abc_def"?