ksh + print the last value from parameter without print the $parameter - bash

I need advice - how to print the same last value in ksh scripts without to print param argument
so what we can do in ksh inorder to print the last value ?
example - I need to print the last value ( in this case -$ETH_PORT ) , without to define $ETH_PORT parameter after the second echo command
how to print the last value from the last echo/print command?
function test
{
ETH_PORT=eth0
echo $ETH_PORT
# now I need to print the last value ( in this case value from $ETH_PORT param )
echo < what need to write in order to print last value >
}
test
expected output after runing the test function
eth0
eth0

You could subvert echo with a function that kept track of the last thing echo'ed, but that wouldn't capture the last thing to go to stdout:
#!/bin/bash
function echo {
/bin/echo $*
last_echoed="$*"
}
function testit {
ETH_PORT=eth0
echo "this is not echo'ed twice"
echo $ETH_PORT
echo $last_echoed
}
testit
A second option is to use a wrapper script that keeps track of what the last line was and then print it at the end.

Related

Not able to assign var to $2 var from command line bash

I have the bash script. I have to assign to second passed var other var, or upper-cased content of the same var. When I try to do sth like this:
x=${2^^}
$2=$x)
I got: line 173: xxx=XXX: command not found
When I try this command : set -- ${2^^}
$2 seems to be..empty. When I echo it, terminal shows empty line.
How to fix it?
You can't assign to $2 directly, yo need to use set. Copy the $# array to a named array, change its second element, and use set to assign the values back:
arr=("$#")
arr[1]=${arr[1]^^}
set -- "${arr[#]}"
printf '%s\n' "$2"
set -- ${2^^} sets the value of upper-cased $2 to $1 and clears the remaining positional arguments.

How to use a passed parameter to call its value as a global variable in Bash script? [duplicate]

This question already has an answer here:
Using variable value as variable name in bash
(1 answer)
Closed 2 years ago.
I am trying to write a function which will take 2 parameters. 1st a color name, 2nd text to be printed.
I have also declared variables for colors as global variables. I want to expand the values by using 1st parameter string.
For now I am using switch case which is the worst way to do it I believe.
Thank you in advance
red=$'\e[1;31m'
grn=$'\e[1;32m'
yel=$'\e[1;33m'
blu=$'\e[1;34m'
mag=$'\e[1;35m'
cyn=$'\e[1;36m'
end=$'\e[0m'
Print() {
# I want to use 1st parameter to call the variables above
# i.e. if red is passed, then i want value of red which is '\e[1;31m'
printf $((${1}))
printf "$2"
printf $end
}
function call
Print red "string"
You are referring to indirect parameter expansion.
Print() {
printf ${!1}
printf "$2"
printf $end
}
However, a safer way to write this is with
Print() {
printf '%s%s%s' "${!1}" "$2" "$end"
}
This ensures you get the expected output even if one of the two arguments (the second in particular) contains a %.
Depending on how many other contexts use your color variables, I would move the escape handling into Print itself, so that you can simply define red=31, for example.
Print() {
printf '\033[1;%sm%s\033[0m' "${!1}" "$2"
}
You have to make bash interpret a string as a variable with ${!...}.
#!/bin/bash
red=$'\e[1;31m'
grn=$'\e[1;32m'
yel=$'\e[1;33m'
blu=$'\e[1;34m'
mag=$'\e[1;35m'
cyn=$'\e[1;36m'
end=$'\e[0m'
Print() {
# I want to use 1st parameter to call the variables above
# i.e. if red is passed, then i want value of red which is '\e[1;31m'
echo -n ${!1}
printf "$2"
printf $end
}
Print "$#"
Try indirect expansion: What is indirect expansion? What does ${!var*} mean?
Here's your function with indirect expansion:
red=$'\e[1;31m'
grn=$'\e[1;32m'
yel=$'\e[1;33m'
blu=$'\e[1;34m'
mag=$'\e[1;35m'
cyn=$'\e[1;36m'
end=$'\e[0m'
Print() {
# I want to use 1st parameter to call the variables above
# i.e. if red is passed, then i want value of red which is '\e[1;31m'
echo "${!1}$2$end"
}
Print red hello

How to create a directory in a function

Editing:
I'm trying to build a simple function that print out a name, then creates a directory. I'm new at building function in bash, so I have the below script that didn't work:
dest_path=/home/all/todo
line="name"
mkdir_for_name() {
echo $1
mkdir $2
}
mkdir_for_name $name
mkdir_for_name $dest_path/$name
What is wrong with that syntax?
Since you are using echo so I believe you want to print directory name; and off course you need to pass 2 arguments to your function. May be call your function in following way. In this way you need not to change your code.
mkdir_for_name "$name" "$dest_path/$name"
Complete script:
dest_path=/home/all/todo
line="name"
mkdir_for_name() {
echo "$1"
mkdir "$2"
}
mkdir_for_name "$name" "$dest_path/$name"
Your function uses $2, but you're only calling it with one argument, so $2 is empty.
You defined a function that accepts 2 arguments, where the first argument passed is the name to be printed on screen (the "echo" line), while the second argument passed will create the desidered folder.
Passing only one argument will just print on screen that argument

How to use/call variables from another function in bash

I am learning to play around with functions in bash. I have the first function read_file() that reads /etc/file and replaces ':'with a space between words (e.g root:x:0:0:root ... becomes root x 0 0 root ... ). I then want to be able to manipulate output from individual words in each of the lines.
My second function- display__user_shell() prints our the shell for each corresponding users as is in the /etc/file.
My problem is figuring out how to call the first function read_file() and using its variables in the display__user_shell function.
I have been able to do the above when using input from a single line rather than reading from a file.
i just called new_data -i.e $new_data from the display__user_shell() function
read_file() {
read -p "Enter file" file
while read line
do
newlin=$(echo $line | tr ":" " ")
echo newlin
done
}
oldIFS=$IFS
IFS=" "
ct=0
display__user_shell() {
readfile
for item in $newlin;
do
[ $ct -eq 0 ] && name="$item";
[ $ct -eq 6 ] && name="$item";
done
echo "$user's shell is $shell"
}
IFS=$oldIFS
display__user_shell
the first line of the output should be..
root's shell is /bin/bash
Irrespective of the implementation there is an interesting question here: how to reference variables from one function in another function. The short answer is that you can:
$ a() { aye=bee; }
$ b() { echo "$aye"; }
$ a
$ b
bee
But this is a very bad idea - Bash has "unfortunate" scoping rules different from safer languages like Java, Python, or Ruby, and code like this is very hard to follow. Instead there are several patterns you can use to produce more readable code:
Print the value in the inner function and assign that to a value in the outer function:
a() {
echo 'bee'
}
b() {
aye="$(a)"
echo "$aye"
}
b # Prints "bee"
Call and assign to a variable the first function in the outer scope and use it in the second function:
a() {
echo 'bee'
}
aye="$(a)"
b() {
echo "$aye"
}
b # Prints "bee"
Treat the first and second functions as a pipeline, passing standard output of the first one to the standard input of the second one (read is a slow way to process a large file, but it'll serve as an example):
a() {
echo 'bee'
}
b() {
while read -r line
do
echo "$line"
done
}
a | b # Prints "bee"
Which one you choose depends on things like what else you intend to do with what a returns and whether a produces huge amounts of output.

Passing and return value of external function in awk

I am calling function from another script from awk.
Why i need to press enter even after adding /dev/null
Why the passed argument is not displayed. I am getting spaces.
I am not getting the return value from external function.
cat mainpgm.sh
#!/bin/bash
key="09"
awk -v dk="$key" ' { ma = system(". /home/ott/functions.sh;derived dk")</dev/null ; "print returned value" ma } '
cat functions.sh
#!/bin/bash
derived () {
echo "outside function" $dk
return 9
}
If you don't want to process input in awk, redirect its input to /dev/null, and do everything in the BEGIN block. Also, for the dk variable to be replaced with its value, it has to be outside the quotes.
awk -v dk="$key" 'BEGIN {
ma = system(". /home/ott/functions.sh;derived " dk);
print "returned value", ma
}' < /dev/null
To answer your questions:
You put /dev/null in the wrong place. It's supposed to be the input of the script, not the system function.
Two reasons: First, you put dk inside the quotes, so its value is not substituted. Second, the derived function doesn't print its argument, it prints $dk, which is a nonexistent shell variable. The argument to a function is $1, so it should do:
echo "outside function $1"
You had print inside the quotes, so it wasn't being executed as a statement.

Resources