combining bourne shell and awk

0

1

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] } }  '

user2692634

Posted 2013-11-18T08:57:35.187

Reputation: 29

You describe that the first script is stored inside a file called WASfile but in the same script you are editing a file of the same name twice (WASfile and with a full path /home/wasadm/WASfile). Is it supposed to be the same file and do you really want to create a self-modifying code? Or did you just unpractically used the same name for the script and for the file being edited? --- I would strongly discourage you from the self-modifying code. You will need much more knowledge to make it working correctly. – pabouk – 2013-11-18T09:48:36.727

Answers

0

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.

pabouk

Posted 2013-11-18T08:57:35.187

Reputation: 5 358

i edited my question as per ur suggestion. Please check. – user2692634 – 2013-11-18T10:41:08.713

@user2692634: Please read my reply. It still applies. If you do not understand something please let me know. Regarding the sed code my older comment still applies.

– pabouk – 2013-11-18T11:07:01.883