Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

learn unix and linux commands

Setting alias for a user - Linux ubuntu

Shell Programming and Scripting


Tags
alias, awk, pkill, sed

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 08-20-2010   -   Original Discussion by joycesolomon
joycesolomon's Unix or Linux Image
joycesolomon joycesolomon is offline
Registered User
 
Join Date: Aug 2010
Last Activity: 20 August 2010, 5:18 PM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Setting alias for a user - Linux ubuntu

Hi

i have a user "SYSTEM"
i want to set the below command in his .profile for an alias:

who | awk '{print $1}'| sed '/SYSTEM/d' | sed '/root/d' |xargs -i pkill -u {}

i tried as below:

alias stop = " who | awk '{print $1}'| sed '/SYSTEM/d' | sed '/root/d' |xargs -i pkill -u {}"

whenever i login as this user and type "stop"
i get the below:

pkill: invalid user name: joyce pts/2 Aug 19 00:34 (jm4tvk1.com)
pkill: invalid user name: joyce1 pts/3 Aug 19 00:35 (jm4tvk1.com)


Any idea why this is failing? Also i added this user in /etc/sudoers

Sponsored Links
    #2  
Old Unix and Linux 08-20-2010   -   Original Discussion by joycesolomon
agama's Unix or Linux Image
agama agama is offline
Registered User
 
Join Date: Jul 2010
Last Activity: 7 April 2014, 3:02 PM EDT
Location: earth>US>UTC-5
Posts: 1,466
Thanks: 110
Thanked 511 Times in 490 Posts
The $1 in your awk script is being expanded (probably to nothing) when you set the alias. This results in the awk being "{print}" rather than {print $1} and the whole line from who is being printed.

try escaping the $ with a back-slant:


Code:
alias stop="who | awk '{print \$1}'|sed '/SYSTEM/d' | sed '/root/d' |xargs -i pkill -u {}"

which should pass the $1 as is into the alias and then execute it properly. You might also want to pipe your output to sort -u before piping it into xargs on the off chance that a user is logged in more than once.
Sponsored Links
    #3  
Old Unix and Linux 08-21-2010   -   Original Discussion by joycesolomon
rdcwayx's Unix or Linux Image
rdcwayx rdcwayx is offline Forum Advisor  
Use nawk in Solaris
 
Join Date: Jun 2006
Last Activity: 13 September 2015, 8:58 PM EDT
Posts: 2,759
Thanks: 44
Thanked 420 Times in 408 Posts


Code:
alias stop="who |awk '! (/SYSTEM/||/root/) {print \$1}' |xargs pkill -u"

    #4  
Old Unix and Linux 08-21-2010   -   Original Discussion by joycesolomon
ygemici's Unix or Linux Image
ygemici ygemici is offline Forum Advisor  
sed_shell@LNU
 
Join Date: Feb 2010
Last Activity: 26 April 2017, 8:59 AM EDT
Location: istanbul
Posts: 1,713
Thanks: 4
Thanked 295 Times in 286 Posts


Code:
alias stop="who | sed '/SYSTEM\|root/d' | awk '{print $1}' | xargs -i pkill -u {}"

Sponsored Links
Closed


Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Alias setting in Solaris jdash.ps Solaris 2 08-02-2010 06:43 AM
First Time Linux(Ubuntu) user, Installing dwainbar UNIX for Dummies Questions & Answers 6 06-08-2010 02:09 AM
Setting an alias in Redhat jxh461 Red Hat 2 05-14-2010 12:59 PM
Query regarding alias and setting bash as a default script VENC22 UNIX for Dummies Questions & Answers 1 07-13-2005 10:18 AM
Setting a boot device alias on Sun hardware 98_1LE UNIX for Dummies Questions & Answers 3 04-12-2002 12:09 AM



All times are GMT -4. The time now is 03:23 PM.