Ksh Storing Result of a Command to a Variable

How to store an output of shell script to a variable in Unix?

Two simple examples to capture output the pwd command:

$ b=$(pwd)
$ echo $b
/home/user1

or

$ a=`pwd`
$ echo $a
/home/user1

The first way is preferred. Note that there can't be any spaces after the = for this to work.

Example using a short script:

#!/bin/bash

echo "hi there"

then:

$ ./so.sh
hi there
$ a=$(so.sh)
$ echo $a
hi there

In general a more flexible approach would be to return an exit value from the command and use it for further processing, though sometimes we just may want to capture the simple output from a command.

How can I store a command in a variable in a shell script?

Use eval:

x="ls | wc"
eval "$x"
y=$(eval "$x")
echo "$y"

How to preserve line breaks when storing command output to a variable?

Quote your variables. Here is it why:

$ f="fafafda
> adffd
> adfadf
> adfafd
> afd"


$ echo $f
fafafda adffd adfadf adfafd afd


$ echo "$f"
fafafda
adffd
adfadf
adfafd
afd

Without quotes, the shell replaces $TEMP with the characters it contains (one of which is a newline). Then, before invoking echo shell splits that string into multiple arguments using the Internal Field Separator (IFS), and passes that resulting list of arguments to echo. By default, the IFS is set to whitespace (spaces, tabs, and newlines), so the shell chops your $TEMP string into arguments and it never gets to see the newline, because the shell considers it a separator, just like a space.

how do I store the output of a cut command into a variable

This command isn't checking the contents of hello.txt, but only the name "hello.txt" itself. Add xargs so that the final command is:

list=$(echo "hello.txt" | xargs cut -f2 -d '/')

As @Kusalananda points out, you can also simplify this further:

list=$( cut -f2 -d/ hello.txt )

how to store result of tail command in variable?

Since tail -f doesn't terminate, you don't want to capture its output in a variable. But since you call it in a loop anyway, call it over and over like this:

OUT=`tail "$path"`

Or using the modern syntax:

OUT=$(tail "$path")

Bash Script store cat output in variable and then echo it

Variable assignments in bash should not have any spaces before or after the equal sign. It should be like this:

#!/bin/bash
var=$(cat tmp/pids/unicorn.pid)
echo "$var"

Which can be written more idiomatically as

#!/bin/bash
var=$(< tmp/pids/unicorn.pid)
echo "$var"

Put select result in a ksh variable

As a basic outline you can run SQL*Plus with a heredoc to perform the query, and assign the output to a variable:

P_NUMBER=`sqlplus -s /nolog <<!EOF
connect username/password
whenever sqlerror exit failure
set pagesize 0
set feedback off
select your_value from your_table where your_key = 'something';
exit 0
!EOF`

Enclosing in backticks assigns the result to the variable. $P_NUMBER will then hold whatever value your query got (or an error message if the credentials were wrong, say). It helps if you're sure the query will return exactly one result. You can also test the return code with $? to look for errors, before you try to use your variable.

Including the -s flag, turning off feedback and setting the pagesize to zero collectively suppress all the noise so you only get the result and don't have to strip out banners, headings etc.

And finally I've used /nolog and put the connect statement inside the heredoc so that the credentials don't appear in the process list, which is an often-overlooked security issue. If you don't want to do that and do put the credentials as sqlplus username/passwd, you can add the -l flag so that it only tries to log in once; otherwise if login fails for some reason it'll try to use the rest of the heredoc as further credentials, and can appear to get hung up with short scripts.



Related Topics



Leave a reply



Submit