Answers to: Bash Scripting Questionshttp://linuxexchange.org/questions/1464/bash-scripting-questions<p>Ok, I've got a basic bash scripting question, but the answer is so simple it eludes me.</p> <p>From my .bash_aliases file....</p> <p>PROBLEM 1:</p> <p><pre><code></p> <p># 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." &amp;&amp; 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." &amp;&amp; echo Rebooting Remote Windows System' #</p> <p></pre></code></p> <p>What variable(s) do I use for the hostname (or IP) and the username?</p> <p>I want to: $ reboot-win-host hostname-here username-here or $ reboot-win-ip ip-here username-here</p> <p>PROBLEM 2</p> <p>For these, I can't get them to work for whatever reasoning....</p> <p><pre><code> 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}'</p> <p>top 25 process/RAM usage alias top-proc='ps aux | awk '{print $2, $4, $11}' | sort -k2rn | head -n 25'</p> <p>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 &lt; $1; i++) {printf("*")}; print ""}' </pre></code></p>enSat, 29 Jan 2011 04:45:25 -0500Answer by pcardouthttp://linuxexchange.org/questions/1464/bash-scripting-questions/1486<p>You already got your answer. If you want to pass command-line arguments to a script, they are called $1, $2, $3, etc.</p> <p>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.</p> <pre><code>#!/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 &amp;&amp; echo Rebooting Remote Windows System' </code></pre>pcardoutSat, 29 Jan 2011 04:45:25 -0500http://linuxexchange.org/questions/1464/bash-scripting-questions/1486Answer by Nominal Animalhttp://linuxexchange.org/questions/1464/bash-scripting-questions/1470<p>Problem 1. Don't use an alias, use a function:</p> <pre><code>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." &amp;&amp; echo "Rebooting Remote Windows system" else echo "Usage: reboot-win-host hostname username" &gt;&amp;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." &amp;&amp; echo "Rebooting Remote Windows system" else echo "Usage: reboot-win-ip IP-address username" &gt;&amp;2 return 1 fi } </code></pre> <hr> <p>Problem 2. Quotes do not nest. In Bash, within a single-quoted string, single-double-single-double-single produces a single quote mark:</p> <pre><code>'It'"'"'s okay' == "It's okay" </code></pre> <p>This is because quoted strings are concatenated directly:</p> <pre><code>abc == 'a'"b"'c' == "abc". </code></pre> <p>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:</p> <pre><code>$USER == "$USER" (iff $USER does not contain whitespace) '$USER' == "\$USER" '"\t"' == "\"\\t\"" </code></pre> <p>Therefore, your aliases should be written as, for example,</p> <pre><code>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 &lt; $1; i++) printf("*"); printf("\n"); }'"'" </code></pre> <p>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).</p>Nominal AnimalFri, 31 Dec 2010 19:35:54 -0500http://linuxexchange.org/questions/1464/bash-scripting-questions/1470