How to use asterisk in cshell alias for ls command? - command-line

Here is the explanation about my question:
normally we use command :
ls *test*
Output of this will be list of all files having "test" string in their name or 'No match'
Here I want an alias like:
lsa test [This alias should expand in this : ls *test* ]
And when I don't give any argument the It should print all files like ls do
lsa [This alias should expand in this : ls ]
I tried with this:
alias lsa "ls '*\!:1**' "
Also like this:
alias lsa 'ls "*\!:1**" '

I got the answer so sharing here:
Above two alias were not working for me then I tried this and this is working.
alias lsa 'ls *\!:1**'
alias lsa "ls *\!:1**"
Working in both the cases : lsa and lsa test
Still I need some explanation why earlier aliases were not working why this one works.

Related

Fish shell wildcard returns different output

I'm trying to create a fish function with named argument contains a wildcard *. But the output between the function I made and the plain command are different.
Here's my function:
function ls-wildcard -a arg
ls $arg
end
and here's the result when I tried to execute it
$ ls-wildcard path/*.foo
bar1.foo
The output shows only 1 file when there should be 2 of them. But the plain ls works like a charm.
$ ls path/*.foo
bar1.foo bar2.foo
Am I missing something?
Edit:
After I tried some different expressions, the behavior of the function seems to terminate itself after the first matched. any way to fix it?
The wildcard is expanded before the function is run.
Your function uses a named argument, which is one argument.
So it is equivalent to this:
function ls-wildcard
ls $argv[1]
end
ls-wildcard path/*.foo
# runs `ls-wildcard` like
ls-wildcard path/bar1.foo path/bar2.foo
and then your function throws away the second argument.
The simplest fix is to just use $argv:
function ls-wildcard
ls $argv
end
which will forward all arguments to ls.

Autocomplete directories in a subfolder with the Fish shell

I'm having trouble getting the 'complete' function in the fish shell to behave as I would like and I've been searching for an answer for days now.
Summary
Essentially I need to provide tab directory auto-completion as if I was in a different directory to the one I am currently in. It should behave exactly as 'cd' and 'ls' do, but with the starting point in another directory. It seems like such a trivial thing to be able to do but I can't find a way to make it work.
Explanation
Example folder structure below
- root
- foo
- a
- dir1
- subdir1
- dir2
- subdir2
- b
- dir3
- subdir3
- dir4
- subdir4
I am running these scripts whilst in the 'root' directory, but I need tab auto-complete to behave as if I was in the 'foo' directory.
testfunc -d a/dir2/subdir2
Instead of
testfunc -d foo/a/dir2/subdir2
There are a lot of directories inside 'foo' and a lot of sub-directories within them, and this auto-complete behaviour is necessary to speed our process (this script is used extensively throughout the day).
Attempted Solution
I've tried using the 'complete' builtin to get this working by specifying the directory to use, but all this managed to do was auto-complete the first level of directories with a space after the argument instead of continuing to auto-complete like 'cd' would.
complete -x -c testfunc -a "(__fish_complete_directories ./foo/)"
Working bash version
I have already got this working in Bash and I am trying to port it over to fish. See below for the Bash version.
_testfunc()
{
local cur prev words cword
_init_completion || return
compopt +o default
case $prev in
testfunc)
COMPREPLY=( $( compgen -W '-d' -- "$cur" ) )
compopt +o nospace
return
;;
-d)
curdir=$(pwd)
cd foo/ 2>/dev/null && _filedir -d
COMPREPLY=( $( compgen -d -S / -- "$cur" ) )
cd $curdir
return
;;
esac
} &&
complete -o nospace -F _testfunc testfunc
This is essentially stepping into the folder that I want, doing the autocompletion, then stepping back into the original folder that the script was run in. I was hoping this would be easier in Fish after getting it working in Bash (I need to support these two shells), but I'm just pulling my hair out.
Any help would be really appreciated!
I am not a bash completions expert, but it looks like the bash completions are implemented by changing directories, running completions, and then changing back. You can do the same in fish:
function complete_testfunc
set prevdir $PWD
cd foo
__fish_complete_directories
cd $prevdir
end
complete -x -c testfunc -a "(complete_testfunc)"
does that work for you?

How can I make a function run every time cd successfully changes to another directory within sh on FreeBSD?

I'm using sh as my shell on FreeBSD but I want to be able to have a pretty prompt like the one bash gives me on Ubuntu. There are two things that the FreeBSD implementation of sh seems to lack as far as PS1 escape characters go:
The \w works but does not expand $HOME to ~, so this is something I have already hacked up myself
I can use PS1 to update the prompt on the terminal, but as far as I can tell it is not possible to use the PS1 variable to update the title bar as well. ESC and BEL fail to set the title as one would expect if they were using bash or ksh
Here is my .shrc file
update_prompt() {
case "$PWD" in
"$HOME"*)
pretty_pwd="~${PWD#*"${HOME}"}"
;;
"/usr$HOME"*)
pretty_pwd="~${PWD#*"/usr${HOME}"}"
;;
*)
pretty_pwd="$PWD"
;;
esac
case "$TERM" in
xterm*|rxvt*)
PS1="[$USER#\\h $pretty_pwd]\\$ "
;;
*)
;;
esac
printf "\\033]0;[%s#$(hostname -s): %s]\\007" "$USER" "$pretty_pwd"
}
update_prompt
So when I fire up a terminal or log in via ssh, it gives the pretty prompt that I like. But now I need this function to run every time that cd is executed and returns an exit status of 0.
I was going to use an alias that was something like:
alias cd="cd $1 && update_prompt"
but that was before I realized that aliases do not except arguments. How might I go about doing something like this?
You can use a function instead of an alias:
cd() {
command cd "$#" && update_prompt
}
Just put it into ~/.shrc. You have to use command here to let sh know that you are referring to the actual cd builtin command instead of the function you've just defined.
Refer to the sh(1) manual page for the details on how to make sh(1) source the ~/.shrc file when it starts:
Therefore, a user should place commands that are to be executed only at login
time in the .profile file, and commands that are executed for every shell
inside the ENV file. The user can set the ENV variable to some file by placing
the following line in the file .profile in the home directory, substituting for
.shrc the filename desired:
ENV=$HOME/.shrc; export ENV
I use this trick in my cd alias manager. Here's a link to the source code of the function: https://github.com/0mp/goat/blob/v2.5.0/libgoat.sh#L31-L57
You can do it with alias+arguments if you swap the commands:
$ alias cd="echo change; cd"
$ pwd
/nas
$ cd /
change
$ pwd
/
$ cd /etc
change
$ pwd
/etc
$

Perl: List files and directories recursively but exclude some directories and files that passed

Please give any suggestion or snippet or anything that may work.
I have already tried wanted function but how do I exclude some directory while recursing?
In Linux, you can make use of the Linux "find" and "grep" commands and run those Linux commands in Perl using qx to store Linux command result in Perl.
e.g.
$cmd = "find . | grep -v 'dir1\|dir2\|...\|dirn'";
$result=qx($cmd);
The above command combinations do the following:
The find command will list the all the directory and
files recursively.
The pipe "|" will pass the find result to grep command
The grep -v command will print on screen only the string not exist
in the "dir1", "dir2"..."dirn" to be ignored
At last, the qx command will execute the find and grep Linux
commands and stored the output to $result variable.
You can do the similar thing in Windows. The only difference is to use the Windows command line.
e.g.
$result=qx('dir /b/s | find /v "workspace" | find /v "TVM"')
The above command will list all the directory recursively except the directory has name "workspace" or "TVM".

iPhone .bashrc not working properly

SOLVED: I had the wrong line endings selected in my text editor
I tried to get a .bashrc going on my iPhone, just for fun. After adding source /var/root/.bashrc to my /etc/profile file I get this every time I log into a terminal emulator, local or over SSH.
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
: command not found
>
My cursor overwrites the '>' everytime I type
The contents of /var/root/.bashrc
alias install='apt-get install'
alias remove='apt-get remove'
alias aptsearch='apt-cache search'
alias respring='killall SpringBoard'
alias safemode='touch /var/mobile/Library/Preferences/com.saurik.mobilesubstrate.dat && killall SpringBoard'
alias shutdown='halt'
alias poweroff='halt'
alias ls='ls -group-directories-first -Ah'
alias lsl='ls -Ah1 --group-directories-first'
alias killall='killall -v'
alias reload='source /var/root/.bashrc'
export PS1='\w> '
clear
The contents of /etc/profile
export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games'
export PS1='\h:\w \u\$ '
umask 022
for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ]; then
. $i
fi
done
source /var/root/.bashrc
Any and all help is appreciated.
SOLVED: I had the wrong line endings selected in my text editor
Try manually executing the script and taking a look at the output. You should get a line number to help you along.