You are not logged in.

#1 2006-01-10 23:34:04

TomE
Member
Registered: 2005-08-06
Posts: 164

options in bash a alias

is there any way to have options in a bash alias?

so for example i want "pacman -s" to alias "sudo pacman -S" so i add
" alias 'pacman -s'='sudo pacman -S' " to my bashrc, but this dont work.

does anyone konw how do to this or am i going to have to mak a script?

Offline

#2 2006-01-10 23:43:20

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: options in bash a alias

I don't think you can have spaces in aliases, IIRC.  It should be possible to do it with a function, but using a script would be approximately equally simple to putting a function in your ~/.bashrc.

alternatively, I just have pacman aliased, and the sudo configuration takes care of options.

Dusty

Offline

#3 2006-01-10 23:55:09

TomE
Member
Registered: 2005-08-06
Posts: 164

Re: options in bash a alias

Dusty wrote:

I don't think you can have spaces in aliases, IIRC

that what i thought

Dusty wrote:

It should be possible to do it with a function, but using a script would be approximately equally simple to putting a function in your ~/.bashrc.

i probably do a srcipt some thing like

#!/bin/bash

if [ $1 ] ; then
         case $1 in
             -s )   sudo pacman -S     ;;
         esac
     else
         echo "i need a option"
fi

and just fill out each option, its a pain in the arse  and i was try to avoid it but... meh

Dusty wrote:

alternatively, I just have pacman aliased, and the sudo configuration takes care of options.

Dusty

i was trying not to do this as i dot want to have to put in my passwd for a -Ss or -Qi

Offline

#4 2006-01-11 00:05:15

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: options in bash a alias

Why not just do
alias pacmans="sudo pacman -S"
or
alias pacinstall="sudo pacman -S"

Offline

#5 2006-01-11 01:07:20

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: options in bash a alias

TomE wrote:

i was trying not to do this as i dot want to have to put in my passwd for a -Ss or -Qi

I hate visudo syntax, but I know I had this at one point. I guess its like this:

Cmnd_Alias  PACMAN = /usr/bin/pacman -[SUAQ]*
dusty   ALL = NOPASSWD: PACMAN

Then I have sudo set up to allow my account to do password-protected sudo on all other options (ie:  dusty   ALL = (ALL) ALL
), so pacman -R, for example, requires a password (so does rm -rf!). Then I have

alias pacman='sudo /usr/bin/pacman'

in my .bashrc.

Dusty

Offline

#6 2006-01-11 14:30:27

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: options in bash a alias

I'm more self-trusting (or maybe reckless) than Dusty, so I have

tomk  tk-i5ka= NOPASSWD: /usr/bin/pacman

in sudoers, and

alias pacman='sudo pacman'

in .bashrc - no password for any pacman commands.

Offline

#7 2006-01-11 15:03:26

TomE
Member
Registered: 2005-08-06
Posts: 164

Re: options in bash a alias

phrakture wrote:

Why not just do
alias pacmans="sudo pacman -S"

that what i have now

Dusty i'll have i look in to the sudo stuff. (but i dont think it will be with NOPASSWD tomk :shock:)

i think i add a function inmy bashrc like dusty said.

pacman () {
case $1 in
    -s )    sudo pacman -S $2 $3 $4 $5        ;;
    -S )    sudo pacman -S $2 $3 $4 $5        ;;
    -su )    sudo pacman -Su $2 $3 $4 $5        ;;
    -Su )    sudo pacman -Su $2 $3 $4 $5        ;;
    -syu )    sudo pacman -Syu $2 $3 $4 $5         ;;
    -Syu )    sudo pacman -Syu $2 $3 $4 $5         ;;
    -R )    sudo pacman -Rs $2 $3 $4 $5        ;;
    -r )    sudo pacman -R $2 $3 $4 $5        ;;
    -* )    pacman $*
esac
}

is any way to do greater than $1 insted that doing "$2 $3 $4 $5"

Offline

#8 2006-01-11 15:19:24

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: options in bash a alias

TomE wrote:

is any way to do greater than $1 insted that doing "$2 $3 $4 $5"

opts=`echo ${@} |sed "s/${1}//"`

then you can use $opts

Offline

#9 2006-01-11 16:18:54

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: options in bash a alias

bash also breaks arguments at ' ', so just "$*" will work fine after you do a 'shift' to remove $1

Offline

#10 2006-01-12 12:50:31

TomE
Member
Registered: 2005-08-06
Posts: 164

Re: options in bash a alias

phrakture wrote:

bash also breaks arguments at ' ', so just "$*" will work fine after you do a 'shift' to remove $1

thanks shift worked

Offline

Board footer

Powered by FluxBB