contents of error.txt:
message1="Reason for error code1"
message2="Reason for error code2"
message3="Reason for error code3.
To solve this, you may try doing restart"
I have a requirement where in I have to extract the contents between the " and save it in a variable for each error code and use it later in my shell script.
(Please note that the messages may be spanned across multi-lines between " as for message3.)
I managed to do it something like:
#!/bin/ksh
## some code here
value=`sed -n '/code3/{N;s/.*"\(.*\)".*/\1/p}' error.txt`
echo "Reason for error code3 is:$value"
Output should look like:
user1@linux:/home/user1> ./error_message.ksh
Reason for error code3 is:Reason for error code3.
To solve this, you may try doing restart
This works in ksh in linux OS. But, I am facing problem in executing the same peice of code in AIX.
$ cat var.txt
message1="Reason for error code1"
message2="Reason for error code2"
message3="Reason for error code3.
To solve this, you may try doing restart"
#Source the file var.txt
$ . var.txt
$ echo $message1
Reason for error code1
$ echo $message2
Reason for error code2
$ echo $message3
Reason for error code3. To solve this, you may try doing restart
As per my requirement, the txt file cannot be executable. So I cannot source it. I have to just read it.:rolleyes:
Is there any other way that I could get the values of the variables.
PS: The "new line character" in the value should be preserved. The value for message3 should come in two lines only.
Sourcing a file does not require it to be executable.
To preserve the carriage return, enclose the variable in quotes:
echo "$message3"
. filename [arguments]
source filename [arguments]
Read and execute commands from filename in the current
shell environment and return the exit status of the
last command executed from filename. If filename does
not contain a slash, file names in PATH are used to
find the directory containing filename. The file
searched for in PATH need not be executable. When bash
is not in posix mode, the current directory is searched
if no file is found in PATH. If the sourcepath option
to the shopt builtin command is turned off, the PATH is
not searched. If any arguments are supplied, they
become the positional parameters when filename is exe-
cuted. Otherwise the positional parameters are
unchanged. The return status is the status of the last
command exited within the script (0 if no commands are
executed), and false if filename is not found or cannot
be read.