Friday 28 June 2019

combining bourne shell and awk


This is my actual file WASfile


#!/bin/sh
sed -i '/^ *$/d' WASfile
sed -i -e '/user=/,/group_1=/{w /tmp/1' -e 'd}' /home/wasdm/WASfile;
Script to Add
read -p "Press [Enter] to continue for Installation"

Now, I want to put the following script at Script to Add above and then execute the WASfile as script(which is combination of many scripts or commands).


#!/usr/bin/awk -f  

BEGIN { FS="=" }
NR==FNR { a[$1]=$0; next }
$1 in a { $0=a[$1] }
/^#/ { var=$1; sub(/^#/, "", var); if(var in a) { $0=a[var] } }
1

I want to combine and use as below or a better way to combine both the scripts.


#!/bin/sh  
sed -i '/^ *$/d' WASfile
sed -i -e '/user=/,/group_1=/{w /tmp/1' -e 'd}' /home/wasdm/WASfile;
#!/usr/bin/awk -f

BEGIN { FS="=" }
NR==FNR { a[$1]=$0; next }
$1 in a { $0=a[$1] }
/^#/ { var=$1; sub(/^#/, "", var); if(var in a) { $0=a[var] } }
1
read -p "Press [Enter] to continue for Installation"

I am unable to execute the Script.


So, I tried to extract the AWKscript to another file and try to execute that AWKscript. But, the problem is after extracting, the main script WASfile itself breaks or fails.


#!/bin/sh
sed -i '/^ *$/d' WASfile;
sed -i -e '/\/usr\/bin\/awk/,/baba/{w 1' -e 'd}' WASfile;
#!/usr/bin/awk -f

BEGIN { FS="=" } NR==FNR { a[$1]=$0; next } $1 in a { $0=a[$1] } /^#/ { var=$1; sub(/^#/, "", var); if(var in a) { $0=a[var] } } 1
baba
read -p "Press [Enter] to continue for Installation"

as below


#./WASfile
sed: -e expression #1, char 24: missing command
./WASfile: line 6: BEGIN: command not found
./WASfile: line 7: {: command not found
./WASfile: line 7: next: command not found
./WASfile: line 8: in: command not found
./WASfile: line 9: syntax error near unexpected token `/^#/,'
./WASfile: line 9: `/^#/ { var=$1; sub(/^#/, "", var); if(var in a) { $0=a[var] } } '

Answer



From your question I understand that your first script is stored in a file called WASfile. You have to make sure that the script has the executable bit set:


chmod a+x WASfile

Then you can execute the script: ./WASfile. Because the current directory is not in the PATH variable by default you have to explicitly specify the path either for the current directory ./ or the absolute path /home/wasadm/WASfile.


The same applies to the AWK script: make it executable and call it with a specified path.


From the WASfile script you can call it the same way as from the command line. The command line is a shell too - either the same or similar as the shell executing the firs script.


#!/bin/sh
sed -i '/^ *$/d' WASfile
sed -i -e '/user=/,/group_1=/{w /tmp/1' -e 'd}' /home/wasadm/WASfile
/path/to/the/AWKscript inputfile1 inputfile2 >outputfile1
read -p "Press [Enter] to continue for Installation"

The code above will run the AWKscript script stored inside /path/to/the directory. With the files as parameters with the self-descriptory names. Put the files you need there.


Another option is to invoke awk explicitly. In such a case you do not need to enable the executable bit of the file.


awk -f /path/to/the/AWKscript

The last piece of code in the question will not work


The combination showed in your last piece of code will not work. Unix-like systems are designed to execute a single executable file by a single interpreter.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...