![]() |
Handling multiple arguments for a command can be difficult, especially when there are a lot of parameters. One way to make this easier is to put the arguments in a text file and use each line of the file as an argument for the command. This can simplify the process and save time. For example, let’s say you have a text file with a list of 100 client names, and you want to create a folder for each client. Writing mkdir <client_name> for 100 clients is not a productive approach. Instead, What if we could pass the text file of client names as an argument to the mkdir command? This article will explore ways to do this. There are three methods to operate. Let’s understand them one by one : Table of Content 1. Using Shell input redirectionWe have a list of clients, stored in a file named clients.txt and we want to create a folder for each client. So instead of giving the name of every client in the mkdir command, we’ll pass the file ‘clients.txt’ as an argument to the mkdir command using the redirection operator ‘<‘. Syntax: command `<FILE_NAME`
You can replace the command with the operation that you want to perform. And FILE_NAME with the name of the file you want to use as an argument. Command: mkdir `<clients.txt`
Output: ![]() Creating a folder for each client using the shell redirection operator We can see after running the command mkdir `< clients.txt` 4 folders for each client Robin, Bob, Martin, and Jack are created. The < operator instructs the shell to read the contents of clients.txt and feed it as input to mkdir.
![]() Rewriting the command inside Limitation of shell input redirectionThe shell redirection operator ‘<‘ has a limitation, it does not care about the spaces or new lines, to be specific it doesn’t have any option to set a delimiter for the input. In clients.txt we have full names of the clients like “Alison George”, “Zora Hood”, when we’ll execute the same command it will generate 4 separate folders named “Alison”, “George”, “Zora” and “Good”, but we want two folders for each client. ![]() Limitation of shell redirection operator Xargs command overcomes this limitation of shell input redirection. 2. Using xargs commandSyntax: xargs [options] command
The xargs command allows us to specify a delimiter on the redirected input using -d option. This capability is useful when you need precise control over how input data is separated and processed. Thus for creating folders with full names the command will be, Command: xargs -d '\n' mkdir < clients.txt
Explanation:
To learn more about xargs checkout xargs command in Linux. 3. Using read command and while loopWe can use a command called “read” to read one line at a time from a file we specify. When we read each line, we also consider any spaces or special characters by using something called “Internal Field Separator” (IFS). Then, we can use a “while” loop to keep doing this for each line in the file, and as we read each line, we use the “mkdir” command to create a new folder with the name we just read. This way, we can create separate folders for each line in the file, ensuring that we handle each line correctly, even if it contains spaces or special characters. Command: while IFS= read -r client_name; Output: Explanation: Let’s understand this line by line, while IFS= read -r client_name;
do: This word marks the beginning of our action in a while loop. Whatever we put after “do” is what we do for each line we read from the file. mkdir “$client_name”: We are creating folder for each client name stored in the variable client_name. done < clients.txt
ConclusionIn this article we discussed how to deal with lots of command parameters, a practical way is to store them in a text file and use each line as a command argument. This saves time and effort. For example, if you have a text file with 100 client names, you can create a folder for each client without manually typing each command. The article explores three methods to achieve this: shell input redirection, xargs command, and read command with a while loop. Each method simplifies using file lines as command arguments, making command-line tasks more efficient. |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |