UNIX, Assign value to a variable in C or KornShell (ksh) - linux

When I use bash to run the following code, it will assign the value 5 to the var1.
var1=$(awk '$1>$3{ print "5"}' newfile2)
echo $var1
But when I use this same code in banana or something, it gives me error. Can someone please tell me if there is some other way I can write this code so I can run it using the C or KornShell (ksh) as well.

For C shell, use
set var=`....`
For bash/ksh
var1=$(awk '$1>$3{ print "5"}' newfile2)

Use backticks and the set command for csh.
set var1=`awk '$1>$3{ print "5"}' newfile2`
echo $var1

Please note that there are no spaces before and after the variable name.
so,
var1=`ls`
is what you need. But, if you have
var = `ls`
you will get errors.
So, your code should be:
var1=`awk '$1>$3{ print "5"}' newfile2`
echo $var1
Make sure you are in BASH shell, not C or TCSH.

Related

Using "read" to set variables

In bash from the CLI I can do:
$ ERR_TYPE=$"OVERLOAD"
$ echo $ERR_TYPE
OVERLOAD
$ read ${ERR_TYPE}_ERROR
1234
$ echo $OVERLOAD_ERROR
1234
This works great to set my variable name dynamically; in a script it doesn't work. I tried:
#!/bin/env bash
ERR_TYPE=("${ERR_TYPE[#]}" "OVERLOAD" "PANIC" "FATAL")
for i in "${ERR_TYPE[#]}"
do
sh -c $(echo ${i}_ERROR=$"1234")
done
echo $OVERLOAD_ERROR # output is blank
# I also tried these:
# ${i}_ERROR=$(echo ${i}_ERROR=$"1234") # command not found
# read ${i}_ERROR=$(echo ${i}_ERROR=$"1234") # it never terminates
How would I set a variable as I do from CLI, but in a script? thanks
When you use dynamic variables names instead of associative arrays, you really need to question your approach.
err_type=("OVERLOAD" "PANIC" "FATAL")
declare -A error
for type in "${err_type[#]}"; do
error[$type]=1234
done
Nevertheless, in bash you'd use declare:
declare "${i}_error=1234"
Your approach fails because you spawn a new shell, passing the command OVERLOAD_ERROR=1234, and then the shell exits. Your current shell is not affected at all.
Get out of the habit of using ALLCAPSVARNAMES. One day you'll write PATH=... and then wonder why your script is broken.
If the variable will hold a number, you can use let.
#!/bin/bash
ERR_TYPE=("OVERLOAD" "PANIC" "FATAL")
j=0
for i in "${ERR_TYPE[#]}"
do
let ${i}_ERROR=1000+j++
done
echo $OVERLOAD_ERROR
echo $PANIC_ERROR
echo $FATAL_ERROR
This outputs:
1000
1001
1002
I'd use eval.
I think this would be considered bad practice though (it had some thing to do with the fact that eval is "evil" because it allows bad input or something):
eval "${i}_ERROR=1234"

create variables inside unix script

I am trying to create a variable in a script, based on another variable.
I just don't know what needs to be adjusted in my code, if it is possible.
I am simplifying my code for your understanding, so this is not the original code.
The code goes like that:
#!/bin/csh -f
set list_names=(Albert Bela Corine David)
set Albert_house_number=1
set Bela_house_number=2
set Corine_house_number=3
set David_house_number=4
foreach name ($list_names)
#following line does not work....
set house_number=$($name\_house_number)
echo $house_number
end
the desired output should be:
1
2
3
4
Thanks for your help.
Unfortunately, the bashism ${!varname} is not available to us in csh, so we'll have to go the old-fashioned route using backticks and eval. csh's quoting rules are different from those of POSIX-conforming shells, so all of this is csh specific. Here we go:
set house_number = `eval echo \" \$${name}_house_number\"`
echo "$house_number"
${name} is expanded into the backticked command, so this becomes equivalent to, say,
set house_number = `eval echo \" \$Albert_house_number\"`
which then evaluates
echo " $Albert_house_number"
and because of the backticks, the output of that is then assigned to house_number.
The space before \$$ is necessary in case the value of the expanded variable has special meaning to echo (such as -n). We could not simply use echo "-n" (it wouldn't print anything), but echo " -n" is fine.1
The extra space is stripped by csh when the backtick expression is expanded. This leads us to the remaining caveat: Spaces in variable values are going to be stripped; csh's backticks do that. This means that if Albert_house_number were defined as
set Albert_house_number = "3 4"
house_number would end up with the value 3 4 (with only one space). I don't know a way to prevent this.
1 Note that in this case, the echo "$house_number" line would have to be amended as well, or it would run echo "-n" and not print anything even though house_number has the correct value.

Parsing a variable in shell scripting

I am new to shell scripting just started off.
I have written this script
#!/bin/sh
profile_type= cat /www/data/profile.conf
echo $profile_type
the o/p of this script is
. /tmp/S_panicA1.txt
. /tmp/S_panicA0.txt
away_Def="panicA1 panicA0"
away_Byp=0
away_Sts=$((panicA1+panicA0-away_Byp))
In this i want to get panicA1 panicA0 and 0 and store it in other variable how to do this?
When you want to assign the output of a command to a variable, you use the dollar parenthesis syntax.
foo=$(cat /my/file)
You can also use the backticks syntax.
foo=`cat /my/file`
In your script, you simply run the command cat and assign its result, nothing, to your variable. Hence the output consisting of the content of your file, result of cat, followed by an empty line, result of echo with an empty variable.

What language runs after I start a Konsole window, and what can it do?

How can I store the result of an an expression into a variable?
echo "hello" > var1
Can I also do something like this?
var1.substring(10,15);
var1.replace('hello', '2');
var1.indexof('hello')
PS. I had tried Googling, but was not sucessful.
As #larsmans comments, Konsole is the terminal emulator, which in turn runs a shell.
On linux, this is typically bash, but it could be something else.
Find out what shell you're using, and print the man page.
echo $SHELL # shows the full path to the shell
man ${SHELL##*/} # use the rightmost part (typically bash, in linux)
For a general introduction, use the wikipedia entry on the unix shell or the GNU Bash refererence
Some specific answers:
var1="hello"
echo ${var1:0:4} # prints "hell"
echo ${var1/hello/2} # prints "2" -- replace "hello" with "2"
And at the risk of showing off:
index_of() { (t=${1%%$2*} && echo ${#t}); } # define function index_of
index_of "I say hello" hello
6
But this goes beyond simple shell programming.
Konsole is bash basically. So its technically bash that you are looking for.
Suppose:
s="hello"
For var1.substring(1,3);
you would do:
$ echo ${s:1:2}
el
For var1.replace('e', 'u');
you can:
$ echo ${s/l/u} #replace only the first instance.
hullo
$ echo ${s//e/u} #this will replace all instances of e with u
For var1.indexof('l')
You can (I dont know of any bash-ish method but, anyway):
$ echo $(expr index hello l)
4
In bash (the standard shell on linux) the syntax for storing the result of an expression in a variable is
VAR=$( EXPRESSION )
so, for example:
$ var=$(echo "hello")
$ echo $var
hello
For your second question: yes, these kind of things are possible using only the shell - but you're probably better of using a scripting language like python.
For what its worth: Here is a document describing how to do string manipulations in bash.
As you can see, it's not exactly beautiful.

Bash Shell - The : Command

The colon command is a null command.
The : construct is also useful in the conditional setting of variables. For example,
: ${var:=value}
Without the :, the shell would try to evaluate $var as a command. <=???
I don't quite understand the last sentence in above statement. Can anyone give me some details?
Thank you
Try
var=badcommand
$var
you will get
bash: badcommand: command not found
Try
var=
${var:=badcommand}
and you will get the same.
The shell (e.g. bash) always tries to run the first word on each command line as a command, even after doing variable expansion.
The only exception to this is
var=value
which the shell treats specially.
The trick in the example you provide is that ${var:=value} works anywhere on a command line, e.g.
# set newvar to somevalue if it isn't already set
echo ${newvar:=somevalue}
# show that newvar has been set by the above command
echo $newvar
But we don't really even want to echo the value, so we want something better than
echo ${newvar:=somevalue}.
The : command lets us do the assignment without any other action.
I suppose what the man page writers meant was
: ${var:=value}
Can be used as a short cut instead of say
if [ -z "$var" ]; then
var=value
fi
${var} on its own executes the command stored in $var. Adding substitution parameters does not change this, so you use : to neutralize this.
Try this:
$ help :
:: :
Null command.
No effect; the command does nothing.
Exit Status:
Always succeeds.

Resources