Please note that LinuxExchange will be shutting down on December 31st, 2016. Visit this thread for additional information and to provide feedback.

Ok, I've got a basic bash scripting question, but the answer is so simple it eludes me.

From my .bash_aliases file....

PROBLEM 1:

# alias reboot-win-host='net rpc shutdown -S thehostname -U username -r -f -t 120 -c "This system will reboot in 2 minutes. Save any work now." && echo Rebooting Remote Windows System' # alias reboot-win-ip='net rpc shutdown -I 111.111.111.111 -U username -r -f -t 120 -c "This system will reboot in 2 minutes. Save any work now." && echo Rebooting Remote Windows System' #

What variable(s) do I use for the hostname (or IP) and the username?

I want to: $ reboot-win-host hostname-here username-here or $ reboot-win-ip ip-here username-here

PROBLEM 2

For these, I can't get them to work for whatever reasoning....


alias perm='stat -c '%A %a %n''
alias list-root-id='awk -F: '($3 == "0") {print}' /etc/passwd'
alias install-date='ls /var/lib/dpkg/info/*.list -lht | tail -1 | awk '{print $6}'

top 25 process/RAM usage alias top-proc='ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 25'

Graphic connections to hosts alias hosts-connect='netstat -an | grep ESTABLISHED | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c \ | awk '{ printf("%s\t%s\t",$2,$1) ; for (i = 0; i < $1; i++) {printf("*")}; print ""}'

asked 29 Dec '10, 22:43

Ron's gravatar image

Ron ♦
9361718
accept rate: 13%




Problem 1. Don't use an alias, use a function:

reboot-win-host () {
    if [ $# -eq 2 ]; then
        net rpc shutdown -S "$1" -U "$2" -r -f -t 120 -c "This system will reboot in 2 minutes. Save any work now." && echo "Rebooting Remote Windows system"
    else
        echo "Usage: reboot-win-host hostname username" >&2
        return 1
    fi
}

reboot-win-ip () {
    if [ $# -eq 2 ]; then
        net rpc shutdown -I "$1" -U "$2" -r -f -t 120 -c "This system will reboot in 2 minutes. Save any work now." && echo "Rebooting Remote Windows system"
    else
        echo "Usage: reboot-win-ip IP-address username" >&2
        return 1
    fi
}

Problem 2. Quotes do not nest. In Bash, within a single-quoted string, single-double-single-double-single produces a single quote mark:

'It'"'"'s okay' == "It's okay"

This is because quoted strings are concatenated directly:

abc == 'a'"b"'c' == "abc".

Different types of quotes do nest, but escapes and variable references within double-quotes are evaluated immediately. Fortunately, you can always escape escapes and variable references, too:

$USER == "$USER"           (iff $USER does not contain whitespace)
'$USER' == "\$USER"
'"\t"' == "\"\\t\""

Therefore, your aliases should be written as, for example,

alias perm="stat -c '%A %a %n'"
alias list-root-id="awk -F: '(\$3 == "0") {print}' /etc/passwd"

# alias install-date='ls /var/lib/dpkg/info/*.list -lht | tail -1 |'" awk '{print \$6}'"
alias install-date='find /var/lib/dpkg/info/ -name "*.list" -type f -printf "%TY%Tm%Td %Ta %Tb %Td %TY\\n" | sort -g | sed -ne "1 s|^[^ ]* *||p"'
alias latest-update-date='find /var/lib/dpkg/info/ -name "*.list" -type f -printf "%TY%Tm%Td %Ta %Tb %Td %TY\\n" | sort -rg | sed -ne "1 s|^[^ ]* *||p"'

alias top-proc="ps aux | awk '{print \$2, \$4, \$11}' | sort -k2rn | head -n 25"

alias hosts-connect="netstat -an | grep ESTABLISHED | awk '{ print \$5 }' | awk -F: '{ print \$1 }' | sort | uniq -c | awk '"'{ printf("%s\t%s\t", $2, $1) ; for (i = 0; i < $1; i++) printf("*"); printf("\n"); }'"'"

Note that I only tested these quickly; you probably should go thorough them to make sure. In the install-date alias, it's better to use find so that it works even with another locale; adjust the printf patterns to your liking (but keep the sort key, %TY%Tm%Td or YYYYmmdd intact).

link

answered 31 Dec '10, 19:35

Nominal%20Animal's gravatar image

Nominal Animal
461
accept rate: 50%

Thanks man, that all works. I've yet to try the Windows rebooting code as of yet. I'll try that and if it works like the rest did, I'll mark the question answered. Thanks for all of the great help. I did +1 it already.

(03 Jan '11, 20:39) Ron ♦

You already got your answer. If you want to pass command-line arguments to a script, they are called $1, $2, $3, etc.

I like the discussion of functions. You could also just use separate scripts. Because $1, $2, etc are not very readable names, I might rewrite your script as follows.

#!/bin/bash
#reboot-win-host.sh
host_name=$1
user_name=$2
comment1="This system will reboot in 2 minutes, Save any work now"

net rpc shutdown -S $host_name -U $user_name -r -f -t 120 -c $comment1 && echo Rebooting Remote Windows System'
link

answered 29 Jan '11, 04:45

pcardout's gravatar image

pcardout
226239
accept rate: 46%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×16
×9
×8

Asked: 29 Dec '10, 22:43

Seen: 4,761 times

Last updated: 29 Jan '11, 04:45

powered by OSQA