Linux FAQ's & Manuals
- Linux Scripts
- Debian Install
- Bash For Beginners
- Bugzilla
- Consultants Guide
- GCC Manual
- Linux Command Line Tools
- Gnu Pascal Coding Standards
- Linux Installation Disk
- Labolatorium Linux(PL)
- Budowa systemu Linux(PL)
- Linux Dictionary
- Network Administrators
- Rescue Disk for Linux
- Red Hat Installation
- Red Hat Customization
- Red Hat Getting Started
- Red Hat Security
- Secure & Optimize
- Slackware Manual
- Suse Support
- Suse FAQ
InfoWorld.nl: "...Queensland has no intention to progress from the six-year-old Windows XP for more than 100,000 computers to be purchased over the next three years..."
Open Solutions Alliance Set To Deliver
internetnews.com: "What is the key to open source solution interoperability...?"
Mandriva Linux 2007 Spring: Spring Is Here
PR: Mandriva is proud to announce the release of Mandriva Linux 2007 Spring. Announcement within.
Thinking Past Platforms: The Next Challenge for Linux
Linux Journal: "In my first SuitWatch Newsletter, on September 5, 2002, I wrote this: 'A funny thing happened to Linux on the way to World Domination: it succeeded..."
Hacking Ubuntu to Improve Performance
ExtremeTech: "The default Ubuntu Dapper Drake installation includes some basic processes that check devices, tune the operating system, and perform housekeeping..."
9.7. the shift built-in
9.7.1. what does it do?
the shift command is one of the bourne shell built-ins that comes with bash. this command takes one argument, a number. the positional parameters are shifted to the left by this number, n. the positional parameters from n+1 to $# are renamed to variable names from $1 to $# - n+1.
say you have a command that takes 10 arguments, and n is 4, then $4 becomes $1, $5 becomes $2 and so on. $10 becomes $7 and the original $1, $2 and $3 are thrown away.
if n is zero or greater than $# (the total number of arguments, see section 7.2.1.2). if n is not present, it is assumed to be 1. the return status is zero unless n is greater than $# or less than zero; otherwise it is non-zero.
9.7.2. examples
a shift statement is typically used when the number of arguments to a command is not known in advance, for instance when users can give as many arguments as they like. in such cases, the arguments are usually processed in a while loop with a test condition of (( $# )). this condition is true as long as the number of arguments is greater than zero. the $1 variable and the shift statement process each argument. the number of arguments is reduced each time shift is executed and eventually becomes zero, upon which the while loop exits.
the example below, cleanup.sh, uses shift statements to process each file in the list generated by find:
#!/bin/bash # this script can clean up files that were last accessed over 365 days ago. usage="usage: $0 dir1 dir2 dir3 ... dirn" if [ "$#" == "0" ]; then echo "$usage" exit 1 fi while (( "$#" )); do if [[ "$(ls $1)" == "" ]]; then echo "empty directory, nothing to be done." else find $1 -type f -a -atime +365 -exec rm -i {} \; fi shift done |
![]() | -exec vs. xargs |
|---|---|
the above find command can be replaced with the following: find options | xargs [commands_to_execute_on_found_files] the xargs command builds and executes command lines from standard input. this has the advantage that the command line is filled until the system limit is reached. only then will the command to execute be called, in the above example this would be rm. if there are more arguments, a new command line will be used, until that one is full or until there are no more arguments. the same thing using find -exec calls on the command to execute on the found files every time a file is found. thus, using xargs greatly speeds up your scripts and the performance of your machine. |
in the next example, we modified the script from section 8.2.4.4 so that it accepts multiple packages to install at once:
#!/bin/bash if [ $# -lt 1 ]; then echo "usage: $0 package(s)" exit 1 fi while (($#)); do yum install $1 << confirm y confirm shift done |
