Concatenate two values using KSH on HP-UX

I need to Concatenate the value of DIR in a while read loop with the value of another field called $counter. Please advise. Thanks.

while read DIR
do
     ?

Hello,

Essentially, you handle variables within a while loop more or less the same way you do anywhere else in a shell script. The fact that they are within a while loop doesn't really change anything, in terms of how you can use variables or the types of things you can do with them.

A couple of quick examples of ways of combining variables together:

#!/bin/bash
fruit=apple
number=1

echo "$fruit""$number"
echo $fruit$number

fruitplusnumber="$fruit$number"

echo "$fruitplusnumber"

And when we run this, we get the exact same output all three times:

$ ./script.sh 
apple1
apple1
apple1
$ 

Hope this helps !

2 Likes

@rtb ,

please supply actual examples of expected input and desired output, @drysdalk has very generously provided a decent example, but ultimately this is for you to solve

NB: we are not here to your solutions but to assist you in coming to those, we may be able to provide alternative once a solution has been presented .... but ultimately if we write your code .... then you've gained very little if anything at all.

Well you know, I am not really asking anyone to write code for me, but if it seems like it, it’s probably because I already searched high and low on the internet for an example, perhaps not realizing I am staring at the answer. Now, I may have missed the answer in my searching the internet, even after browsing through topics on this site, but I did exhaust all attempts to find a solution before posting a topic on this site. Another reason is as I already stated in previous posts, that I am really new to scripting. So, your patience and the patience of others would be appreciated.

Well, I just read your other post which I commented on, and it is hard to believe what you said above.

If you want to write a while statement in bash, then you simply google for a reference,

Then, you quickly find a tutorial you like and you follow it.

For example:

https://www.cyberciti.biz/faq/bash-while-loop/

If you want to concatenate strings in bash, you do the same thing.

This is how we all write code. If we do not know the correct syntax, we just go find a reference and get the info we need.

You may not realize how lucky we are today. In the old days when we programmed, before the Internet was full of reference sites, we actually had to buy a book to program in a language. Now, you just google for a reference or use a code editor with syntax checking like Visual Studio Code, where you can type "while" and the reference pops up in VSC so no need to even google.

When I learned to code, I spend thousands of dollars on reference books. Now, those days are long, long behind us. We have IDEs, code editors, and google. There is no need to go to the library and find a reference on C++ because we do not want to spend $60 on another book. It's all free on-line.

What is so hard about finding a reference on how to concatenate in bash?

I simple do not understand @rtb what you are doing? If you have no idea how to code, that's fine, but the first thing you should decide on is what shell or programming language you want to code in. Then, you look at a reference in that language you like and then use the syntax / commands of that language.

it's really simple, believe me.

Are you using bash? What are you trying to do?

Yes, but you need to at least decide what language you are going to write your script in.

bash is a good choice, so let's go with bash.

Now, that we have decided on bash, you should just google, as I have mentioned:

concatenate two strings in bash

You will find a million references. You do not need our team to spoon feed you what google yields in milliseconds.

Then, you should, in good faith write code in bash to do what you want, based on using a reference. We are not a replacement for using a programming language reference. They are everywhere and free online.

Then after you write code, in good faith, using one or more references, you can post here if you have questions.

Do you understand @rtb?

We are not a substitution for a bash reference guide. That's not the best use of our time and expertise. You are very lucky drysdalk is so generous and one of the nicest people on the planet :). On the other hand, munkeHoller is correct as well. You must write your own code (by selecting a language such as bash and reading the reference for the commands you think you need) and try writing code, in good faith.

Here ya' go:

Bash Reference Guide

The operating system is HP-UNIX. The scripting is not BASH, but KSH. I’ll try to be more precise when looking for something on the internet.

I learned and programmed KSH on HP-UX extensive nearly 35 years ago.

Do you have a KSH reference guide @rtb ?

If so, which one(s) are you using?

I used to script exclusively in KSH, but have not played with that shell in a very long time. I use BASH for sys admin tasks, generally speaking.

Since you are not experienced at this, you might be better off scripting in BASH on HP-UX, but I guess you will need to install it first.

See, for example, this old reference. You can surely find newer install guides for BASH on HP-UX.

No, I do not have a ksh reference guide.

Honestly, because people have "moved away" from KSH over the years, it's hard to find good KSH refs on the net.

If I were you, I would serious consider installing BASH and learning BASH.

There are very good BASH reference guides and tutorials on the net.

If you "insist" on KSH, the here is a reference guide for you:

Even if there is a BASH shell that could be downloaded, the UNIX administrators where I work would not allow me to do that.

Yes, this is common on many HP-UX production applications.

So, check out this reference or find another one:

You must learn the basics of KSH syntax as it is not really our role to teach you what you can easily read on a KSH reference page.

We are here to help you after you write your own KSH script where you have actually tried to use basic KSH command and syntax from a KSH reference.

Why?

Well, many of us will google the same reference for KSH to respond to your questions, because we generally do not memorize the syntax of all programming language we use. When I was in engineering school, almost all tests were "open book" because we were expected to use references to solve hard problems, not memorize language specific syntax and commands. We did not go to the university to learn to memorize syntax and solve small problems. We learned how to reference others work and solve complex problems.

So, you @rtb should do the same as we do. Review the KSH reference guides when writing KSH code. Write your own KSH code "in good faith" and post back here valid KSH problems when you are writing KSH code based on using one or more KSH reference guides.

Let me also add that I am sure you @rtb have seen the Matrix (the first one).

In that movie, Neo did not go to the net and ask others to show him "kung fu". Someone plugged "kung fu" into his brain. Since we don't have a "KSH chip" to plug into our brains like in the Matrix, we need to read the basic reference material for KSH as we use it.

I am sure you can understand this point, @rtb .

From my perspective, I woke up this AM to see someone who was asking about scripting (in two posts at the same time) who did not even identify the shell or the OS they were using. I read team members guessing and assuming what you were doing. All you had to do, @rtb was to tell everyone you were on HP-UX and were using KSH and a description of your task. This would not have wasted your time or our time.

Personally, I do not understand why anyone would post in a forum, asking experts questions, and not include the OS and programming language. You cannot order parts for "a car". You must specific the make "BMW", The model "Z4 E89" and the year (2012). The same is true for computers. You must specific the basic "make" and "model" and "version", or at least the "make" (HP-UX) and the "model" (KSH). You actually should specify the KSH version :wink:

ksh --version

The version of HP-UX would also be useful :slight_smile:

The shell does not have a string-concatenation operator. You simply chain the two strings.
As was shown in post 2.

Use (double-)quotes around a $var in commands, to avoid an unwanted expansion.

var1="word"
var2="word2"
echo "$var1""$var2"
echo "$var1$var2"

Run this on the command line!
echo is a command.

In the following case you must indicate where the variable name ends:

var1=word
echo "$var1xxx"
echo "$var1"xxx
echo "$var1""xxx"
echo "${var1}xxx"
echo "$var1"'xxx'

Play a bit on the command line, use your own variables!

1 Like

some 'reference' sources

if you REALLY want to help yourself [and colleagues] invest in a [couple] of paperbacks
(O'Reilly) Korn shell books are available on amazon/ebay for a fraction of their original selling prices

https://www.qenesis.com/training/shell_programming/kshCheatsheet.pdf
http://kalwin.fr/docs/pdf/unix/unix-korn-shell.pdf (en francais - but you'll get the gist)
http://www.st23.org/misc/sysadmin/downloads/reference.card-ksh.programming.pdf
https://jkmaterials.yolasite.com/resources/materials/UNIX/UNIX_SHELL_PROGRAMMING/UNIT-IV-%20USP.pdf
https://geodesy.geology.ohio-state.edu/course/refpapers/korn_script_0105.pdf
https://azizsaya.wordpress.com/2018/03/24/korn-shell-cheat-sheet/

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.