when trying to create file in unix which has $ in that file it is throwing a number? - unix

trying to create a file with following data using shell script.
InsertParam.sh
echo "$$Domain=XYZ" >parameter.prm
when i run InsertParam.sh
Am getting out put as
$cat parameter.prm
1979205Domain=XYZ
Please help me how to over come this in my parameter.prm
i need Data as
$$Domain=xyz

In sh/bash/ksh/zsh, $$ is the current PID. see https://www.gnu.org/software/bash/manual/bashref.html#Special-Parameters
You need to use different quotes to prevent that variable from being expanded:
echo '$$Domain=XYZ' >parameter.prm
see https://www.gnu.org/software/bash/manual/bashref.html#Quoting
Quotes can be mixed, as required:
echo '$$Domain='"$domain" >parameter.prm

Related

Assign HDFS location of Hive external table to a Unix variable

Is there a way to get hdfs location from external table and assign to unix variable.
Yes, there is a way.
Run a command like the below,
export location=hive -e "SHOW CREATE TABLE <dbname.tablname>;"| grep hdfs://
Please use after the equal sign on the above command and at the end. It is not shown here due to Stackoverflow's rich text format.
You can check the variable like the below,
echo $location
'hdfs://hostname:8020/HDFS/PATH/OF/THE/TABLE'
Hope this helps!

How to make SFTP cozbatch return different error codes

I need to get different SFTP exit codes for each error. For instance 'no such file or directory' --> exit code=552 or 550 instead of returning 1.
I've tried the following and it did not work:
//A05FTP EXEC PROC=SFTPROC,COND=(0,NE)
//COPSFTP.MYSTDIN DD *
host="xpto.xpty.xptz"
lzopts mode=text
cd /home/apl/files/unl
ls
a=`ls | wc -l`
echo `$a`
echo $?
QUIT
//*
and the output in spool is:
cozsftp> lzopts mode=text
mode=text
cozsftp> lzopts mode=text
mode=text
cozsftp> cd /home/apl/files/unl
Ý09.807¨ Invalid command.
cozsftp> a= 1
CoZBatchÝI¨: returning rc=exitcode=1
Can anyone help me?
COZBATCH allows you to embed shell scripts into JCL, so you don't need to use BPXBATCH. BPXBATCH really is a poor utility. If you're using Co:Z then good for you it rocks.
If you want to run shell commands you need to use the ! escape character.
!echo $a
FWIW, SFTP always returns 1 on error. I'm not sure if you can change that. Errors should be logged in the sysout.
Your problem may simply be the echo `$a`. Try enclosing with quotes instead of tick marks.
More generally, if you want to do more detailed error checking, instead of using the SFTP procedure (SFTPROC), I think you'd do better to write yourself a simple script that you execute with BPXBATCH. The script would issue the same SFTP commands, but you could capture and redirect the output (STDOUT/STDERR) and based on the return value ($?) and any error messages, you could certainly detect all the unusual conditions you might want.

Unix Bourne-shell environment variable

I want to create a .profile file that will have a welcome message everything I log in. However, I need to set the environment variable which I don't understand. Are you able to tell me what it does and how to do it?
To display all environment variables and their values:
env
To change or set an environment variable:
export var=value
To remove an environment variable:
unset var
Typically, a welcome message can be shown by customizing the /etc/motd file to your liking. If you want, you can also add some messages in /etc/profile by using the echo or print commands.
For example:
echo "Welcome ${USER}"
or
echo "Welcome $(whoami)"
Code:
$ VAR=Hello
$ echo $VAR
Output:
Hello
Ref link.

unix sh script - read from file

I'm trying to read the content from a aux file, but I can't figure why the command don't work, if I use the string in parameter, that was read from read from file..
Script
file=servers.aux
for server in $(cat $file)
do
echo $server
echo $server
`/usr/IBM/WebSphere/App/profiles/BPM/bin/serverStatus.sh $server -username adm -password adm`
done
Result
BPM.AppTarget.bpm01.0
ServersStatus[7]: ADMU0116I:: not found.
In past, I used something like: put the variable in one array and read the variable from that array, but I think this is possible, what I'm doing wrong?
Thanks in Advance
Tiago
I don't think you need the back-ticks on the last line. You're not trying to run the output of the serverStatus.sh script as a command itself, are you?

change file extension in unix

I am taking a intro to Unix class and am stuck on the final assignment. I need to write a script to change the file extension of a filename that is input when the script is run. The new file extension is also input when the script is run. The script is call chExt1.sh . Our first trial of the script is run as follows
./chExt1.sh cpp aardvark.CPP
The script is suppose to change the second input file extension to the file extension given in the first input. It is not suppose to matter what file extension is given with the file name or what file extension is given as the new extension, nor is it only for changing uppercase to lowercase. In hope to make this very clear if given the following:
./chExt1.sh istink helpme.plEaSe
The script would change helpme.plEaSe to helpme.istink . I have searched on this forum and in google and have had no look with trying the different examples I found. Below is some of the examples I have tried and what I currently have.
Current
#!/bin/sh
fileExtension="$1"
shift
oldName="$2"
shift
newName=${oldName%%.*}${fileExtension}
echo $newName
The echo is just to see if it works, and if I get it working I'm going to add an mv to save it.
Others that I have tried:
newName=`${oldName%.*}`
newName=`${oldName#.*}`
sed 's/\.*//' $oldName > $newName
I can't seem to find some of the other sed I have used but they involved alot of backslashes and () with .* in there. I did not try the basename command cause I don't know the file extension to be entered and all I the examples I saw required that you specify what you wanted removed and I can't. I did not list all the different quote variations that I used but I have tried alot. My instructions say to use the sed command since we should know how to use that from class but when I try to do it I don't isolate just the ending of the file and I believe (cause it takes so long to finish) that it is going through the whole file and looking for .'s and anything after cause I kept doing .* as the pattern. Thanks for anyhelp you can give.
shift shifts the positional parameters, so after calling shift the second parameter ($2) is now the first ($1). The second shift is not necessary, because you are done accessing the parameters. You need to either remove the shift
#!/bin/sh
fileExtension="$1"
oldName="$2"
newName=${oldName%%.*}${fileExtension}
echo $newName
or change $2 to $1.
#!/bin/sh
fileExtension="$1"
shift
oldName="$1"
newName=${oldName%%.*}${fileExtension}
echo $newName
However, you are still missing a dot from your new file name. That is left as an exercise for the reader.

Resources