Note that there are some explanatory texts on larger screens.

plurals
  1. POCommand aliases in my script
    primarykey
    data
    text
    <p>I am creating a script for copying files or directory with a date attached to its name, for example if the file name is <code>test</code> it will be <code>test-20130901.bkup</code></p> <p>and this is my script</p> <pre><code>#!/usr/bin/bash set -x getopts fd TYPE [ $TYPE = "d" ] &amp;&amp; alias cp="cp -r" backup_error() { echo "${0##*/}: $1" exit -1 } typeset -r FROM_DIR=$2 TO_DIR=$3 if [ ! -e $FROM_DIR -a ! -d $FROM_DIR ] || [ ! -e $TO_DIR -a ! -d $TO_DIR ] then backup_error "One of the directories isn't exist or it maybe a file"; fi typeset -r DATE=$(date "+%Y%m%d") for filename in $FROM_DIR/* do if [ -$TYPE $filename ] then cp $filename $TO_DIR/${filename##*/}-$DATE.bkup fi done unalias cp </code></pre> <p>In the script I check if the user wants to run the script on files only or on directories only. <code>-f</code> for files only <code>-d</code> for directories only</p> <p><code>[ $TYPE = "d" ] &amp;&amp; alias cp="cp -r"</code>, this line to check if the script runs for directory I must use <code>cp -r</code> so I make an alias for <code>cp</code> to be <code>cp -r</code> </p> <p>but when I use <code>set -x</code> to debug I find the when a user use <code>-d</code> option the <code>cp</code> command in the <code>if</code> still the original one not the alias.</p> <p>Debugging:</p> <pre><code>&gt; ./backup.sh -d . . + getopts fdb TYPE + '[' d = d ']' + alias 'cp=cp -r' + typeset -r FROM_DIR=. TO_DIR=. + '[' '!' -e . -a '!' -d . ']' + '[' '!' -e . -a '!' -d . ']' ++ date +%Y%m%d + typeset -r DATE=20130901 + '[' -d ./backup.sh ']' + '[' -d ./dir1 ']' + cp ./dir1 ./dir1-20130901.bkup cp: ./dir1: is a directory + '[' -d ./file1 ']' + '[' -d ./file2 ']' + '[' -d ./test.sh ']' + unalias cp </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload