Dealing with Arguments in Shell Script - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

demo-image

Dealing with Arguments in Shell Script

Share This

In C programming language, probably you have heard of command line arguments. In shell script, we can pass arguments in the similar way. The ith argument is received by $i, for example, the first argument is received by $1. The number of arguments is determined by $#.

Copy content of a file to another

In the following example, the name of the source and destination files are received as arguments.

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
if [ $# -eq 2 ]
then
cp $1 $2
else
echo "Usage: program.sh <source_file> <dest_file>"
fi
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Happy Exploring!

Comment Using!!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.