Behavior of ECHO Command and Variable Usage in Unix - unix

Can't post the image directly yet, so here's a link to it:
http://i.imgur.com/sjPVNU6.png
I'm trying to learn some basic unix file scripting for an Informatica project I'm working on. I've used a few scripts in the past and have only a very rudimentary idea of what to do so I'm just playing around with the various parts of an existing script.
I can't make heads or tails of some of the behavior of the commands. Here is an example using ECHO. The thing appears to just randomly return one of: the variable I defined, the variable name, or an error.
e.g. why does:
$ _Src="/home/axxxxxx"
$ echo "${_Src}"
return
/home/axxxxxx
but
$ INFA_HOME="/home"
$ echo "${INFA_HOME}"
returns
ksh: "${INFA_HOME_^H}": bad substitution
other inscrutable behavior:
$ echo "${INFA_HOME} now"
>
$ echo "${INFA_HOME} now"
/home now
$ echo "${INFA_HOME} "
{INFA_HOME}
$ echo "${INFA_HOME} now"
/home now

Looks like your terminal has some issues. Make sure echo $TERM agrees with the emulation mode of the terminal, and that locales etc. are correctly set up.

I think you should check your putty or terminal settings. You may see in the image below the string works as expected.
http://i.stack.imgur.com/5CQ1j.png
the screenshot that you shared shows "^H" which is equivalent to backspace, so the chances you might have used that. The next time you use the variable it prints the output as expected.
Even with the first variable all goes fine.

Related

Zsh update prompt with time

I'm trying to update the previous command prompt with the time when the command was written.
With the code I wrote, typing the following command
[--:--] costam:~ $ echo "Wrote this at 10:20"
and launching it after five minutes, results in this output:
[10:25] costam:~ $ echo "Wrote this at 10:20"
Wrote this at 10:20
The code in the zsh-theme to achieve this, is the following
PROMPT='[--:--] %{$fg[red]%}$USER:%{$reset_color%}%{$fg[green]%}%c%{$reset_color%}$(git_prompt_info) %(!.#.$) '
preexec () {
DATE=`date +"%H:%M"`
echo -e "\r\033[1A[${DATE}]"
}
The problem is when the command exceed one line or there's an activated virtual env. In such cases, the prompt is not overridden where it should, and results in this:
# Multiline
[--:--] costam:~ (master) $ cat ~/.oh-my-zsh/custom/themes/davever
[10:20]-theme
# Virtualenv
[10:20]env) [--:--] costam:~ (master) $ echo "Broken"
Broken
A solution I was thinking of, was of finding the current prompt in preexec() and then replace [--:--] with the current time, but I don't know how or if is even possible to get the current prompt.
Any other solution is welcome, as long as the time is printed in the same way (or very similar) and not, for example, printing on the right side using RPROMT or printing in the new prompt.
Thank you!
You can just use %T to get the current time in your prompt sequence.
Additionally, rather than put raw escape codes in your prompt, you can simplify your prompt string by using %F{<color>} and %f instead.
So, remove that preexec function and use this as your prompt:
PROMPT='[%T] %F{red}$USER:%F{green}%c%f$(git_prompt_info) %(!.#.$) '
What if you asked ZSH to refresh the terminal prompt with the current time periodically?:
PROMPT='[%D{%L:%M}] %{$fg[red]%}$USER:%{$reset_color%}%{$fg[green]%}%c%{$reset_color%}$(git_prompt_info) %(!.#.$) '
TMOUT=1
TRAPALRM() {
zle reset-prompt
}
Sample output which tracks the current time on the computer, down to the second:
[11:41] costam:~ $
%D{%L:%M} will have the same effect as your use of date +"%H:%M". TMOUT suggestion found here. Since you only want the hour and minute, you might be able to increase the TMOUT value to 10 seconds or more.

sleep inside inotifywait in a shell function not working

I am trying to run the following function
foo () {
sleep 1
echo "outside inotify"
(inotifywait . -e create |
while read path action file; do
echo "test"
sleep 1
done)
echo "end"
}
Until inotifywait it runs correctly; I see:
>> foo
outside inotify
Setting up watches.
Watches established.
However as soon as I create a file, I get
>>> fooo
outside inotify
Setting up watches.
Watches established.
test
foo:6: command not found: sleep
end
Any idea why? Plus do I need to spawn the subprocess ( ) around inotifywait? what are the benefits?
thank you.
Edit
I realized I am running on zsh
The read path is messing you up, because unlike POSIX-compliant shells -- which guarantee that only modification to variables with all-uppercase names can have unwanted side effects on the shell itself -- zsh also has special-cased behavior for several lower-case names, including path.
In particular, zsh presents path as an array corresponding to the values in PATH. Assigning a string to this array will overwrite your PATH as well.

What does terminal / hyperterm display in the tab title? Is it possible to customize this?

I'm confused what data is being shown in my terminal tabs. The first part, epzio is obviously my username. But the second part, #C02S60BBG8WP, I have no idea.
Note: I use zsh combined with oh-my-zsh for my shell.
Is it possible to customize what is shown in the tab titles? I'd like to remove the epzio#C02S60BBG8WP part altogether and just display the current working directory.
Also, why do my tab titles turn blue for no apparent reason? I thought this was only supposed to happen when you had a specific process such as node running. As you can see in the screenshot below, the tic-tac-toe tab has turned blue even though nothing is happening in that tab.
Update: It seems that the solution might involve making changes to ~/.oh-my-zsh/lib/termsupport.zsh, but I'm not sure if these would get overridden when oh-my-zsh updates itself.
C02S60BBG8WP is probably your hostname; check by typing hostname.
You can change the terminal title by printing an escape sequence like this:
echo -en "\033]0;New terminal title\a"
So this should change the title to your current working directory, $PWD, substituted by a single ~ if you're in $HOME:
echo -en "\033]0;${PWD/#$HOME/~}\007"
If that doesn't work, it's probably being overridden immediately afterwards by a command that is automatically invoked by your shell. In bash, this would be PROMPT_COMMAND which on my system looks like this:
$ echo $PROMPT_COMMAND
__vte_prompt_command; printf "\033]0;%s#%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"
The zsh equivalent seems to be to define a precmd hook:
precmd() { echo -en "\033]0;${PWD/#$HOME/~}\007" }
To make that permanent, you can just put it your .zshrc.

zsh function only runs once

I'm fairly new to zsh (oh-my-zsh) and i'm trying to write a custom theme.
I ran into a problem and reduced it to the following testcase
PROMPT='$RANDOM > '
works as expected, it produces a random number on each command.
But when using a function
PROMPT='$(my_random) > '
function my_random(){
echo $RANDOM
}
it always returns the same number, even after source ~/.zshrc still the same number. only when i close the terminal window and open it again, i get a new number which stays the same for the complete session.
only when i do:
PROMPT='$RANDOM $(my_random) > '
function my_random(){
echo $RANDOM
}
i get two random numbers as expected... any explanation for this behaviour?
btw, i'm using kde's konsole on a fresh arch install.
Edit
fwiw i found using /dev/urandom directly works well. I would still like to know whats going on.
function my_random() {
echo $(cat /dev/urandom | tr -dc '0-9' | head -c5)
}
$()-expansion happens in a subshell, and changes to $RANDOM in a subshell don’t affect the parent. From zshparam(1):
The values of RANDOM form an intentionally-repeatable pseudo-random sequence; subshells that reference RANDOM will result in identical pseudo-random values unless the value of RANDOM is referenced or seeded in the parent shell in between subshell invocations.
You don’t need to turn to setting the prompt to reproduce it:
% echo $(echo $RANDOM)
17454
% echo $(echo $RANDOM)
17454
bash doesn’t share zsh’s behaviour here.
The annoying bit is that prompt expansion also happens in a subshell, so you can’t just fix this by referencing $RANDOM in, say, precmd. The best way I can find is to do it in an empty expansion:
PROMPT='${RANDOM##*}$(my_random) > '
As said in a comment by chepner, you can fix this by putting : $RANDOM; in your precmd. This causes the value of $RANDOM to be taken and a new one to be generated.
e.g.
precmd() {
: $RANDOM;
...
}

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.

Resources