Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm still getting around the quirks of shell scripting, but I feel like I'm getting a little more comfortable with it.

I've been trying to create an alias to generate a tar file that I have to create frequently, but it always gives me tar errors. Just as a test I tried to create a simple wrapper but it doesn't work either:

function eztar(){

    if [ -d $1 ]
    then
        export QTAR="$1.tar";
        tar –cvf ${QTAR} "$1/";

    else
        echo "Missing directory name";
    fi

}

>eztar mydir
tar: invalid option -- ''
Try `tar --help' or `tar --usage' for more information.

Is the -cvf option in this case getting eaten up somehow?

Anyway here'es another one

I had a problem where Apache would run out of memory. I found out that too many failed shutdowns cause the semaphore to get full so to fix that I discovered a script I liked and I tried to get it to run in alias. (BTW is there a better way to do this?) I think I need to put this in a function because when I check the aliases the for loop had expanded to the list of current semaphores, which isnt what I had intended.

alias semfix='eval "for i in `ipcs -s # grep apache # awk '{print $2}'` ; do ipcrm -s $i; done";'

I'm learning I cant just create an alias or wrapper function out of everything on Unix!

share|improve this question

2 Answers 2

up vote 2 down vote accepted

There's nothing wrong with your first function except there's a typo.
The character in front of the cvf isn't a minus char.
Are you using a std. US 101 key keyboard?

Taking that out altogether (the minus is optional on most tar switches) and the function works fine.
Or else, you could fix it with a proper -cvf

Also, there's no need for the export
export causes an environment variable to have more persistence than you need in a function like this.

I suggest you read the Advanced Bash-Scripting Guide
Look at let, local and export.
See how they vary, when to use one or the other.

Yes, you should try and rewrite the second as a function.
The less nested quoting and piping, while learning, will make writing functions and aliases easier.

The one liners come later :)

share|improve this answer

Try using tar without the minus sign, i.e. just tar cfv

It's the original, now non-standard format.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.