0
votes

I am new to linux and its shell programming.

I wrote a shell script on ubuntu and linux mint which is working perfectly on their systems.

Now when I tried to run this shell script on red hat , whole output is weird.

Problem:

  1. \n characters are printed as it is , no new line is being printed.

  2. some errors occuring like expr:non-integer argument.I have used only three types of statements for comparison -

    1.     if [ "$proto" = "TCP" ]
    
    2.     if [ "$prot_no" = "06" ]
    
    3.     if [ "$i" -eq 32 ]
    

I don't know where exactly is the error but there was no error on ubuntu and mint systems.

I have heard that Default shell in ubuntu is bash while in red hat is ksh but I am not sure about this.

If this is the case then how can I change my shell or if possible is there any command by which I can run my script through bash shell so that there would not be any need to make any change in the script.

Please help me .... It is almost impossible to change all \n characters in the script.

Edit:

  1. for \n issue this is a sample line ..

    echo "\n\n$proto Header"
    
  2. for other errors here is some code which may possibly help ...

     if [ "$trans_or_tunn" -eq 1 ]
    
     then
    
    prot_no=`head -n 1 decrypt.txt | awk -F " " '{printf "%s",$7}'`
    
    if [ "$prot_no" = "06" ]
    then
            proto="TCP"
    
    else if [ "$prot_no" = "11" ]
         then
            proto="UDP"
    
         else if [ "$prot_no" =  "3a" ]
                    then
                    proto="ICMP"
    
               fi
          fi
    
       .....
    

    echo "\n\n$proto Header" this line was supposed to print one of the three values .. TCP HEADER,UDP HEADER or ICMP HEADER.

but what is actually printed out on screen is ..

Output on screen for above line \n\nHeader

4
Thanx for a quick response but script is having around 1000 lines of code.. how can I do that ??? .If possible tell me any way through which I can run the same script on red hat without changing the script.Udit Gupta
To diagnose the \n issue we'll need to see sample code where you attempt to print the newline. The non-integer argument is due to a variable expanding to a non-integer value or being empty; if you know which line this error occurs on that would help. The default shell doesn't matter as long as your script's bang line is correct. If you need bash specify bash.sorpigal
@Sorpigal please see my edit .Udit Gupta
I can only suppose that $trans_or_tunn does not contain an integer. I imagine it contains the string expr. Where's the code that assigns to it? Have you tried turning on set -x?sorpigal

4 Answers

4
votes

Change the first line of the script from

#!/bin/sh

to

#!/bin/bash

Use echo -e to get echo to print \n as a newline.

Use [[ ]] instead of [ ] for tests.

Explanation: the default shell on Ubuntu is dash, not bash. This is a rather limited (but therefore fast) shell, that is unfortunately not available on all Linux platforms. bash, not ksh, was the default shell on Red Hat the last time I checked, and is available almost everywhere, so portable shell scripting requires either targeting that or writing scripts to a common subset of bash, dash and ksh. (If you're interesting, the best portable subset is roughly that defined in the POSIX standard shell command language.)

0
votes

You can change all \n characters like:

cat file | sed 's/\n/somethingelse/g' > newscript

If you have a problem with file transfer (through windows (winspc...etc, known line endings issue)
This might help you:

Convert a.txt and write to e.txt.

          dos2unix -n a.txt e.txt

you can invoke bash to make sure you're running bash (if it's installed in desired OS).

0
votes

To enable automatic expansion of backslash-escape sequences you can add this line to your script after the interpreter line:

shopt -s xpg_echo

The 'bash' manpages offer further insight.

0
votes

printf is more portable than echo, and newlines work properly:

printf "\n\n%s Header\n" "$proto"

As for if [ "$i" -eq 32 ], what is in $i?

The error message mentions expr -- are you calling that program in your script?