24.07.2018 Views

Bash-Beginners-Guide

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Bash</strong> <strong>Guide</strong> for <strong>Beginners</strong><br />

You picked archive-20030129 (1)<br />

#?<br />

Setting the PS3 prompt and adding a possibility to quit makes it better:<br />

#!/bin/bash<br />

echo "This script can make any of the files in this directory private."<br />

echo "Enter the number of the file you want to protect:"<br />

PS3="Your choice: "<br />

QUIT="QUIT THIS PROGRAM - I feel safe now."<br />

touch "$QUIT"<br />

select FILENAME in *;<br />

do<br />

case $FILENAME in<br />

"$QUIT")<br />

echo "Exiting."<br />

break<br />

;;<br />

*)<br />

echo "You picked $FILENAME ($REPLY)"<br />

chmod go-rwx "$FILENAME"<br />

;;<br />

esac<br />

done<br />

rm "$QUIT"<br />

9.6.2. Submenus<br />

Any statement within a select construct can be another select loop, enabling (a) submenu(s) within a menu.<br />

By default, the PS3 variable is not changed when entering a nested select loop. If you want a different prompt<br />

in the submenu, be sure to set it at the appropriate time(s).<br />

9.7. The shift built-in<br />

9.7.1. What does it do?<br />

The shift command is one of the Bourne shell built-ins that comes with <strong>Bash</strong>. This command takes one<br />

argument, a number. The positional parameters are shifted to the left by this number, N. The positional<br />

parameters from N+1 to $# are renamed to variable names from $1 to $# - N+1.<br />

Say you have a command that takes 10 arguments, and N is 4, then $4 becomes $1, $5 becomes $2 and so<br />

on. $10 becomes $7 and the original $1, $2 and $3 are thrown away.<br />

If N is zero or greater than $#, the positional parameters are not changed (the total number of arguments, see<br />

Section 7.2.1.2) and the command has no effect. If N is not present, it is assumed to be 1. The return status is<br />

zero unless N is greater than $# or less than zero; otherwise it is non-zero.<br />

Chapter 9. Repetitive tasks 118

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!