0

I am trying to store the content of a text file in one variable and using that variable as body in email sending. Below is the code to store the value in variable

ct=`cat a.txt`

I tried with

ct="`cat a.txt`"

The issue is, when I am executing the script as sh script.sh. The whole contents of a.txt are getting stored in variable ct.

But when I am executing the script without the sh extension (i.e. script.ksh). Only few contents are storing is variable ct. I am not able to trace what the issue is. Need your help.

bdulac
  • 1,686
  • 17
  • 26
PANKAJ
  • 1
  • 1
  • 2
  • Thanks for your response David. I tried with same option but same result. I am passing this variable to another script which is used to send email – PANKAJ Jul 01 '15 at 07:03
  • Sure. Let me know if you need more help. – David C. Rankin Jul 01 '15 at 07:04
  • This issue is coming only when I am executing the script as "script.ksh" and "./script.ksh". This is working fine when script is executed as "sh script.ksh". Is there any environmental file change required – PANKAJ Jul 01 '15 at 07:09
  • What is the **top line** in your script? `#!/bin/sh`? You have made your script executable right? `chmod 0755 script.ksh` ? If so `./script.ksh` should work. – David C. Rankin Jul 01 '15 at 07:13
  • Does your script have a "shebang line" (something starting with `#!`) ? If yes, check that line. If no, see what shell you are currently running with `ps`. You can also try to run the script is sh by giving `sh` as one command, execute `./script.ksh` and give `exit` to come back to your normal shell. – Walter A Jul 01 '15 at 07:15
  • Yes, "shebang line" is there #!/bin/ksh. And script has executable rights also. But still same output. – PANKAJ Jul 01 '15 at 07:29
  • Does [Capturing multiple line output to a Bash variable](http://stackoverflow.com/questions/613572/) help? If not, show your code that is using `$ct`. – Jonathan Leffler Jul 01 '15 at 07:29
  • Can you change `#!/bin/ksh` into `#!/bin/sh` ? Perhaps the sh really is bash, and can do more. You said `sh script.ksh` works, and that way the interpreter on the first line is ignored. I bet the `ksh script.ksh` fails. – Walter A Jul 01 '15 at 08:16
  • You can also try to make a small testscript like `set -vx; ct=$( – Walter A Jul 01 '15 at 08:19
  • Yes, I changed the shebang line #!/bin/ksh into #!/bin/sh . It's working fine. In Unix #!/bin/ksh works fine, but in CentOS it was giving this problem. Is there any major change in behaviour of korn shell in Unix and Linux ? – PANKAJ Jul 01 '15 at 08:50
  • Most things work the same for Linux ksh, when installed. See my answer. – Walter A Jul 01 '15 at 09:11

1 Answers1

0

On default Linux systems the /bin/ksh is not installed. Sometimes you can install a ksh package. The work-around ln -s /bin/bash /bin/ksh is dangereous, since bash is a superset most of the times, but is not always compatible.

For the CentOS environment the best way would be shanging the shebang line to #!/bin/bash, but be aware of the following issues:

  • Version control
    When you want to use the same scripts on CentOS and other Unix OS, you need to maintain two versions or get bash installed on the other Unix OS.
  • Developers should write in one language, bash or ksh
    When you need to maintain large ksh scripts, writing new scripts in bash will become confusing.
  • Check/test your code
    Do you set variables inside while-loops? They can get lost!
Walter A
  • 19,067
  • 2
  • 23
  • 43