Huu Phan | Blog Linux operating system | Linux operating system -Blog Huu Phan | www.huuphan.com

This Blog is protected by DMCA.com

huuphan.com. Powered by Blogger.

Bash script arguments from a file content

In this tutorial, how to change string content of file as arguments. I written  Bash script arguments from a file content as below:
Input file
How to change NAME and DOMAIN in input file with any arguments in content as below.
My name: NAME
My Blog: https://DOMAIN.com
Output file with arguments default value
My name: Huu
My Blog: https://huuphan.com
Output file with arguments another vaule 
My name: Devops
My Blog: https://devopsroles.com
My bash script 
$ cat arguments_file.sh
The content bash script
#!/bin/bash
argument1=Huu
argument2=huuphan
INPUT=/home/huupv/scripts/input.txt
OUTPUT=/home/huupv/scripts/output.txt
RED='\033[0;31m'
NC='\033[0m' # No Color
if [ $# -eq 2 ];then
    argument1=$1
    argument2=$2
fi
sed -f - "$INPUT" > "$OUTPUT"<<EOF
    s/NAME/$argument1/g
    s/DOMAIN/$argument2/g
EOF
Running bash script with default value
$ ./arguments_file.sh
The output as picture below:

Bash script arguments from a file content

To run bash script with 2 arguments
$ ./arguments_file.sh Devops devopsroles
The display as picture below:
Bash script arguments from a file content - www.huuphan.com



post new :

Huu Phan | Blog Linux operating system | Huu Phan ~ Zimbra Mail Server,linux,bash script,centos,linux command | www.huuphan.com