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:
-
\n characters are printed as it is , no new line is being printed.
-
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:
-
for
\nissue this is a sample line ..echo "\n\n$proto Header" -
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 HEADERorICMP HEADER.
but what is actually printed out on screen is ..
Output on screen for above line
\n\nHeader
Change the first line of the script from
to
Use
echo -eto getechoto print\nas a newline.Use
[[ ]]instead of[ ]for tests.Explanation: the default shell on Ubuntu is
dash, notbash. This is a rather limited (but therefore fast) shell, that is unfortunately not available on all Linux platforms.bash, notksh, 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 ofbash,dashandksh. (If you’re interesting, the best portable subset is roughly that defined in the POSIX standard shell command language.)