0

My shell script contains a function which is used to print statements to another shell script

    function print()
    {
    echo "
    insert()          #insert function
    for i in $InsertFile1
    do
    cat $i 
    done
    echo \""Exit--------------------------0"\"
    switch($input)
    {
     case 1 :
     ......
     ......
     default :
    }
    " > Script.sh

    }

The variable $InsertFile1 = file1.txt and contents of file1.txt is as follows :

insert() #insert function echo "1 Start" echo "2 Stop" echo "3 Exit" switch($input) { case 1 : ...... ...... default : }

The expected output should in Script.sh should be the contents of variable $InsertFile1 but my output (i.e. content of Script.sh) is

 insert()          #insert function
    for i in echo "1    Start"
    echo "2 Stop"
    echo "3 Exit"
    switch($input)
        {
         case 1 :
         ......
         ......
         default :
        }

How do I skip the printing of for loop in file Script.sh ? edit : The function print contains several other statements which needs to be included in Script.sh.

  • Are you trying to loop over the contents of the file? If so, why not simply cat "$InsertFile1" > Script.sh and then append the "Exit ... " line? A loop seems superfluous here. – steeldriver Jul 20 '16 at 11:35
  • The function contains other statements above and below the loop which also needs to be included in the Script.sh – dragonfire_007 Jul 20 '16 at 12:11
  • 1
    In which case you need to append the contents of the file (>> instead of >) - regardless of whether you use a loop or not. I suggest you step back and describe what you actually want to achieve, rather than focusing on a specific implementation - see What is the XY problem? – steeldriver Jul 20 '16 at 12:18
  • @steeldriver The question has been edited now I hope it makes more sense now. – dragonfire_007 Jul 20 '16 at 12:55
0

try

function print()
    {

    for i in $InsertFile1
    do
    cat $i 
    done > Script.sh
    echo "Exit--------------------------0" >> Script.sh

    }
  • you may wish to skip line with echo Exit
  • if $Insertfile1 have only one value, script could be further simplified

    function print()
    {
    
    cp $Inserfile   Script.sh
    
    echo "Exit--------------------------0" >> Script.sh
    
    }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.