I want the current command to be shown in the title of screen (or tmux).
I tried following settings but it doesn't work.
How can I make it work?
.screenrc
shelltitle "$ |fish"
shell /usr/local/bin/fish
.config/fish/config.fish
set -x PS1 '\033k\033\\[\u#\h \W]\$ '
For fish version 2.1.0 you only have to edit ~/.config/fish/functions/fish_title.fish
function fish_title
hostname
end
For version 1.23.1 this doesn't seem to work. If the directories do not exist, first create them:
mkdir -p ~/.config/fish/functions/
What worked for me in .config/fish/functions/fish_title.fish :
function fish_title
# this one sets the X terminal window title
# argv[1] has the full command line
echo (hostname): (pwd): $argv[1]
switch "$TERM"
case 'screen*'
# prepend hostname to screen(1) title only if on ssh
if set -q SSH_CLIENT
set maybehost (hostname):
else
set maybehost ""
end
# inside the function fish_title(), we need to
# force stdout to reach the terminal
#
# (status current-command) gives only the command name
echo -ne "\\ek"$maybehost(status current-command)"\\e\\" > /dev/tty
end
end
I think you're looking for fish_title. See documentation here.
You could do something like this:
function fish_title
echo $_ ' '
pwd
end
funcsave fish_title
(Note you just run this at a prompt - don't put it in a config file).
Thanks for your answers.
Finally, this made it work!
.screenrc
shelltitle "$ |fish"
shell /usr/local/bin/fish
.config/fish/config.fish
function fish_prompt
echo -ne '\033k'
echo -ne $argv
echo -ne '\033\\'
echo -ne '$ '
end
Related
I'm writing a program in Unix to have the user enter in the file they would like to view the contents on but i'm stuck and dont know way i keep getting error.
the errors i keep getting are :unexpected EOF while looking for matching `"'
and the other is: Syntax error: unexpected end of file
# this program allows the user to see the contents of a file
echo
clear
echo
echo "Enter in the the file you would like to see: "
read $1
if [ ! -e "$1" ]
then
echo cat /export/home/cna397/logname/$1
else
echo "This file does not exist
fi
You're missing an ending double quote here:
echo "This file does not exist
$1 is for commandline arguments. You'll need something like this if you want the user to enter a filename while the script is running:
read filename
echo $(cat "/export/home/cna397/logname/$filename")
this is the executable (without errors) version of what you wrote, now continue with a better basis, if you still have questions just update this post with code plus comments, or just make a new one.
echo "Enter in the the file you would like to see:"
read file_name
if test ! = $file_name
then
echo $(cat /export/home/cna397/logname/$file_name)
else
echo "This file does not exist"
fi
P.S if on the if test you are checking strings, keep the equal (=) i put....
In pure /bin/sh how can I distinguish between an empty variable, an unset variable and a not existing (not defined) variable.
Here are the case:
# Case 1: not existing
echo "${foo}"
# Case 2: unset
foo=
echo "${foo}"
# Case 3: Empty
foo=""
echo "${foo}"
Now I would like to check for each of those three cases.
If case 2 and case 3 are actually the same, then I must at least be able to distinguish between them and case 1.
Any idea?
UPDATE
Solved thanks to Matteo
Here is how the code looks like:
#foo <-- not defined
bar1=
bar2=""
bar3="a"
if ! set | grep '^foo=' >/dev/null 2>&1; then
echo "foo does not exist"
elif [ -z "${foo}" ]; then
echo "foo is empty"
else
echo "foo has a value"
fi
if ! set | grep '^bar1=' >/dev/null 2>&1; then
echo "bar1 does not exist"
elif [ -z "${bar1}" ]; then
echo "bar1 is empty"
else
echo "bar1 has a value"
fi
if ! set | grep '^bar2=' >/dev/null 2>&1; then
echo "bar2 does not exist"
elif [ -z "${bar2}" ]; then
echo "bar2 is empty"
else
echo "bar2 has a value"
fi
if ! set | grep '^bar3=' >/dev/null 2>&1; then
echo "bar3 does not exist"
elif [ -z "${bar3}" ]; then
echo "bar3 is empty"
else
echo "bar3 has a value"
fi
And the results:
foo does not exist
bar1 is empty
bar2 is empty
bar3 has a value
I dont know about sh, but in bash and dash you can do echo ${TEST:?Error} for case 1 vs. case 2/3. And from quick glance at wikibooks, it seems like it should work for bourne shell too.
You can use it like this in bash and dash (use $? to get the error code)
echo ${TEST:?"Error"}
bash: TEST: Error
[lf#dell:~/tmp/soTest] echo $?
1
[lf#dell:~/tmp/soTest] TEST2="ok"
[lf#dell:~/tmp/soTest] echo ${TEST2:?"Error"}
ok
[lf#dell:~/tmp/soTest] echo $?
0
[lf#dell:~/tmp/soTest] dash
$ echo ${TEST3:?"Error"}
dash: 1: TEST3: Error
$ TEST3=ok
$ echo ${TEST3:?"Error"}
ok
You can use ${var?} syntax to throw an error if var is unset and ${var:?} to throw an error if var is unset or empty. For a concrete example:
$ unset foo
$ test -z "${foo?unset}" && echo foo is empty || echo foo is set to $foo
-bash: foo: unset
$ foo=
$ test -z "${foo?unset}" && echo foo is empty || echo foo is set to $foo
foo is empty
$ foo=bar
$ test -z "${foo?unset}" && echo foo is empty || echo foo is set to $foo
foo is set to bar
I don't see a correct answer here yet that is POSIX compliant. First let me reiterate William Pursell's assertion that the code foo= is indeed setting the variable foo to an empty value the same as foo="". For foo to be unset, it must either never be set or unset with unset foo.
Matteo's answer is correct, but there are caveats. When you run set in bash and posix mode is disabled, it also prints all of the defined functions as well. This can be suppressed like this:
isvar() (
[ -n "$BASH" ] && set -o posix
set | grep -q "^$1="
)
By writing it as a sub-shell function, we don't need to worry about what the state of bash's posix setting after we're done.
However, you can still get false-positives from variables whose values contain carriage returns, so understand that this is not 100% foolproof.
$ a="
> b=2
> "
$ set | grep ^b=
b=2
So for maximum correctness you can exploit bash's -v test, when available.
isvar() {
if [ -n "$BASH" ]; then
[ -v "$1" ]
else
set | grep -q "^$1="
fi
}
Perhaps somebody has a library somewhere that supports other shells' extensions as well. Essentially, this is a weakness in the POSIX specification and it just hasn't been seen as warranting amendment.
You can use set
If no options or arguments are specified, set shall write the names and values of all shell variables in the collation sequence of the current locale. Each name shall start on a separate line, using the format:
You can list all the variables (set) and grep for the variable name you want to check
set | grep '^foo='
When I run the grep command on the command prompt, the output is correct. However, when I run it as part of a script, I only get partial output. Does anyone know what is wrong with this programme?
#!/bin/csh
set res = `grep -E "OPEN *(OUTPUT|INPUT|I-O|EXTEND)" ~/work/lst/TXT12UPD.lst`
echo $res
Your wildcard is probably being processed by the shell calling awk rather than as part of the awk script.
try escaping the * with a \ (i.e. \*)
In the following example, why do I get an extra slash \ at the end of the string.
[root#server src]# echo 'testme one more word new line' | ./redis-cli -x set mytest
OK
[root#server src]# ./redis-cli
redis> get mytest
"testme one more word new line\"
In the above example, I do not want the \ in "line\". It is not there in the original echo statement.
What I'm getting is not a backslash, but a break line (backslash+n).
That is added by the "echo" command. You can use echo -n to avoid that extra break line:
$ echo -n 'testme one more word new line' | ./src/redis-cli -x set mytest
OK
$ ./src/redis-cli get mytest
"testme one more word new line"
How can I check the return value of "Find" statement in shell script
I am use Find in my script , if find statement don't find any file the execute exit !!
I want to check the return value of "Find" if it found any files or not
You can redirect output of the find command to a file called say output.txt then you can check if the size of that file is 0 or not by using -s option;
if [[ -s "output.txt" ]]
then
echo "File is not empty!"
else
echo "File is empty!"
fi
You can count the number of files found by find using the wc -l command:
export result=`find . -name *.txt | wc -l`
You can now check result to see how many files where found
if [ $result == "0" ]; then echo zero found; fi