LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Setting up alias in bash (https://www.linuxquestions.org/questions/linux-newbie-8/setting-up-alias-in-bash-4175424766/)

iwambeke 08-30-2012 08:46 AM

Setting up alias in bash
 
Ok so in csh I have the following alias's and what they do
Code:

alias cl 'cd \!* && ls -alh'
alias g 'cl /proj/\!:1/rev\!:2/workarea/<user>/

The objective for cl is cd and list everything. g will go to a certain project and revision example g P09 45
goes to /proj/P09/rev45/workarea/<user>/. The g command is because I am constantly switching between projects and revisions.

Sometimes I need to use bash so I have tried setting up alias's in my .bashrc file exactly the same but as you know the \!* doesn't work. So far I have the following
Code:

alias cl="xargs -I {} bash -c 'cd {} && ls -alh' <<<"
This will print the contents in the specified location, but not stay in that location.

Any thoughts on how to write a file in bash to perform the same as in csh?

What I read bash says "bash has no mechanism for using argument in the replacement text, as in csh. If arguments are needed, a shell function should be used." I didn't really want to use a function though, I know someone out there can help me out, it is really close to being correct I think. Someone smart help me out here.

iwambeke 08-30-2012 08:50 AM

Sorry for the double post still new to linux and this forum don't know how to delete message

Snark1994 08-30-2012 08:53 AM

I'm not sure you could do it with an alias, but you could do it with a shell function :)

Just add to your .bashrc:

Code:

g(){
    cd $1
    ls -alh
}

EDIT: better not to double-post, just edit your last post. Also, what? Why don't you want to use a shell function? It works the same as an alias, essentially, but just is more powerful...

iwambeke 08-30-2012 12:46 PM

Ok great I am really new to everything in Linux. I was trying to use an alias because it is really easy to do in csh. That function looks simple, and works great.

Now I am struggling with the second part, for csh first I have
Code:

alias g 'cl /home/\!:1/
alias g2 'cl /home/\!:1/rev\!:2/areas/<user>/

writing a function that can detect if 2 inputs or one are present so here is attempt but it doesn't work
Code:

cl(){
 cd $1
 ls -alh
}

g(){
  if["$2" = "??"] || ["$2" = "?"];then
    cl /home/"$1"/rev"$2"/areas/$USER/
  else
    cl /home/"$1"/
  fi
}

but the response
bash: /home/iwambeke/.bashrc: line 32: syntax error near unexpected token `then'
bash: /home/iwambeke/.bashrc: line 32: ` if["$2"="??"] || ["$2"="?"];then'

chrism01 08-30-2012 06:09 PM

1. please use code tags when posting https://www.linuxquestions.org/quest...do=bbcode#code

2.
Code:

g()
{
if [[ $# -eq 2 ]]
then
    cl /home/"$1"/rev"$2"/areas/$USER/
else
    cl /home/"$1"/
fi
}

https://www.linuxquestions.org/quest...script-201221/
http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

PS Add
Code:

set -xv
into the top of a prog to see what the parser is doing (after the shebang line)

David the H. 08-30-2012 06:22 PM

First, please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, bolding, colors, or other fancy formatting.

Second, this...
Code:

alias cl="xargs -I {} bash -c 'cd {} && ls -alh' <<<"
...doesn't work because it spawns a new shell instance. The environment changes made there (i.e. pwd) are lost when that shell exits, so you end up back where you started.

Also, remember that an alias is just a text substitution feature, not a command. If the first token (word) on the line matches a preset alias, the text of the alias is inserted at that point. Everything after the first token stays where it is. So there's no way to position arbitrary arguments that way. That's what makes shell functions so much more flexible. They're basically scripts that execute inside the current environment.

As for your second one, first, understand that "[" is a command (an synonym for test), and everything after it is an argument to that command, so they need to be whitespace separated. Also, test doesn't have pattern-matching ability.

As long as you're using bash, you should use the new "[[" keyword instead, which is broadly compatible, but more flexible, and does support pattern matching.

Speaking of which, I'm not really sure what you are doing here:
Code:

[ "$2" = "??" ]
This appears to be trying to match a single, two letter string. Is that right? But not only is it the old test, it's also quoted, which disables the globbing power of "?". Or perhaps this is some attempt to carry over syntax from csh.

For pattern matching two characters (of any kind) in bash, you'd use this:
Code:

[[ $2 == ?? ]]
But I'm not sure this is what you really want anyway. I think you just want to know if a $2 entry exists. So just use this:

Code:

if [[ $2 ]]; then
I suggest also putting in some sanity code too, such as a test to see if the directory exists before changing to it.

See the following links for more on bash tests. When using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/ArithmeticExpression
http://mywiki.wooledge.org/BashFAQ/031
http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression
http://wiki.bash-hackers.org/syntax/arith_expr


All times are GMT -5. The time now is 08:39 AM.