How to Get to the Command Line on a Mac. In this Article: Using Launchpad Using Spotlight Using Finder Community Q&A This wikiHow teaches you how to open the Terminal (command prompt) application in macOS using Launchpad, Spotlight, or Finder.
Active5 years ago
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
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:
My Bash program:
Chnossos
6,22911 gold badge1919 silver badges3131 bronze badges
User1996User1996
5 Answers
This line:
doesn't do what you want. And your
mv line is broken too.
Also, your C program doesn't exit with an error when an error is detected.
Also,
sed and tr are existing programs that are suitable alternatives to writing your C program to transliterate (translate) characters in strings.
Also, Ignacio Vazquez-AbramsIgnacio Vazquez-Abrams
rename /prename are existing (Perl) programs that handle rename files with regular expression pattern functionality to rename files, which may be already available on your system(s).
613k111111 gold badges11081108 silver badges11961196 bronze badges
In your specific example, I would not shell out to a custom C program to do this.
Here's an equivalent shell script (not requiring
tr , sed or any programs besides bash and mv ):
In your specific problem, you are not setting your nneonneonneonneo
VAR properly. Shell scripts cannot accept spaces around the = when setting variables, and you need to use backticks or $() to execute an external program. So, properly written, you want
133k2828 gold badges215215 silver badges310310 bronze badges
If you just want the results than I would suggest simply replace the bash script and custom C program with a single short Bourne or POSIX shell script.
Otherwise
You want the shell script to run your C program (I'll refer to it as
todash for simplicity) before setting the shell variable VAR . This is done using the backtick ` (located near upper right corner of US keyboards with tilde, '~') operation.
For todash.c I'll suggest a couple of mostly small improvements.
The added length is only my additional inline comments.
The largest change was untangling the
argc if comparison from the while loop, which once simplified, become a classic example of where to use a for loop.
And for your sanity, and those around you, use braces (curly brackets) around conditional blocks of code. Just because you are allowed to not include them, does not mean you should (not include them). Programs tend to live beyond their original intention and expand in the future. Avoid making mistakes later by including them now.
mctylrmctylr
I for the most part second mctylr, but recommend an even cleaner version of the loop:
Such is the power of array pointers. Now you save ~4 bytes and have some pretty slick code to boot.
This is far from being just a facelift though. Your code is actually very dangerous because if 'argv[1]' is less than 256 characters, you are reading arbitrary memory past the array bounds and potentially writing to it (in a hypothetical scenario where filepath is of length 0, the next 255 whitespaces in memory are going to be changed to underscores.)
![]()
Instead, check whether this equates to true:
... and exiting with an error if it does. Then read argv[1].
Please correct me if I got anything wrong, I'm by no means an expert.
BridgeBridge
2 problems with the mv $1 'C_program '$@'
Sample Graduation Program Script
however this can be done more efficiently with
technosaurustechnosaurus
How To Write A Program Script
6,22011 gold badge2222 silver badges4545 bronze badges
Not the answer you're looking for? Browse other questions tagged cbashcommand-line-arguments or ask your own question.
Write a bash script so that it receives arguments that are specified when the script is called from the command line. Use this method when a script has to perform a slightly different function depending on the values of the input parameters, also called arguments.
Example of Passing Arguments in a Bash Script
If you developed a script called stats.sh that counts the words in a file, it's best to pass the file name as an argument so that the same script can be used for all the files that will be processed. For instance, if the name of the file to be processed is song list, enter the following at the command line:
Arguments are accessed inside a script using the variables $1, $2, $3, and so on. The variable $1 refers to the first argument, $2 to the second argument, and $3 to the third argument. This is illustrated in the following example:
For readability, assign a variable with a descriptive name to the value of the first argument ($1), and then call the word count utility (WC) on the variable $FILE1.
If you have a variable number of arguments, use the $@ variable, which is an array of all the input parameters. This procedure uses a for loop to iteratively process each one, as illustrated in the following example:
Here's an example of how to call this script with arguments from the command line:
If an argument has spaces, enclose it with single quotes. For example:
Flags Method
Frequently a script is written so that arguments can be passed in any order using flags. With the flags method, some of the arguments can be made optional.
For example, you want to write a script that retrieves information from a database based on specified parameters, such as username, date, and product, and generates a report in a specified format. The script needs to be written in such a way so that these parameters are passed when the script is called. It might look like this:
Bash enables this functionality with the getopts function. For the above example, use getopts as follows:
This is a while loop that uses the getopts function and a so-called optstring — in this case u:d:p:f: — to iterate through the arguments. The while loop walks through the optstring, which contains the flags that are used to pass arguments, and assigns the argument value provided for that flag to the variable option. The CASE statement then assigns the value of the variable option to a global variable that is used after all the arguments have been read.
Meanings for ColonsWedding Program Script
The colons in the optstring mean that values are required for the corresponding flags. In the above example of u:d:p:f:, all flags are followed by a colon. This means all flags need a value. If, for example, the d and f flags were not expected to have a value, u:dp:f would be the optstring.
A colon at the beginning of the optstring (for example, :u:d:p:f:) has a completely different meaning. It handles flags that are not represented in the optstring. In that case, the value of the option variable is set to ? and the value of OPTARG is set to the unexpected flag. This behavior displays a suitable error message informing you of the mistake.
How To Write A Program Script For Command Line On Mac Screen
Arguments that are not preceded by a flag are ignored by getopts. If flags specified in the optstring are not provided when the script is called then nothing happens, unless you specially handle this case in your code. Any arguments not handled by getops can still be captured with the regular $1, $2, and $3 variables.
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |