LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-05-2015, 06:05 PM   #1
whim
LQ Newbie
 
Registered: Oct 2014
Distribution: CentOS
Posts: 16

Rep: Reputation: Disabled
How to handle parameters in a .bashrc function


I want to create a simple bash function (in .bashrc) that takes in one parameter (a directory name) and tars all the files inside of that directory into a specific directory in /home (we'll call that destdir). The name of the tar should be the same as the directory parameter.

For example, I want to be able to type into the terminal "task directory_name".

I tried this but it didn't work:

Code:
function_name() {
   cd $1
   tar czf /~/destdir/$1.tar *

   return 0
}
alias task=function_name
What am I doing wrong?
 
Old 02-05-2015, 07:49 PM   #2
ulto
Member
 
Registered: Apr 2004
Posts: 52

Rep: Reputation: 1
Try it by taking out the / in the /~/destdir part. ~/destdir should be sufficient.

Also, trying creating a shell script from that function and use bash -x to diagnose the issue further if the above doesn't help

Last edited by ulto; 02-05-2015 at 07:50 PM.
 
Old 02-05-2015, 07:49 PM   #3
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,308

Rep: Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566
Code:
function_name() {
cd $1
tar -czf ~/destdir/$1.tar.gz .
 }
alias task=function_name
Will cd into $1 then create an archive in ~/destdir.
N.B.
Won't handle folders with spaces in their names in ( that's for you too work out)
~/destdir must exist if it doesn't again for you to work out.
See the man page for tar an the advanced bash scripting guide for detailed help
 
Old 02-05-2015, 07:50 PM   #4
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,308

Rep: Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566
Quote:
Originally Posted by ulto View Post
Try it by taking out the / in the /~/destdir part. ~/destdir should be sufficient.
Sorry you posted while I was typing
 
Old 02-05-2015, 07:53 PM   #5
ulto
Member
 
Registered: Apr 2004
Posts: 52

Rep: Reputation: 1
SNAP! Ah no worries
 
Old 02-06-2015, 08:58 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,144

Rep: Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350Reputation: 1350
Code:
function_name() {
cd $1 || return 1
tar -czf ~/destdir/$1.tar.gz .
 }
alias task=function_name
If you type a wrong name and the cd fails, you really don't want tar acting in the old CWD.
 
Old 02-06-2015, 09:10 AM   #7
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,308

Rep: Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566
Quote:
Originally Posted by rknichols View Post
Code:
function_name() {
cd $1 || return 1
tar -czf ~/destdir/$1.tar.gz .
 }
alias task=function_name
If you type a wrong name and the cd fails, you really don't want tar acting in the old CWD.
+1
Yes lots of error checking can be done but leave something for the OP to do!

My excuse is I was typing on my tablet and couldn't actually test the code!

Last edited by Keith Hedger; 02-06-2015 at 09:11 AM. Reason: PS
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] function with variables in .bashrc 111 Linux - Newbie 9 07-04-2013 08:34 AM
Function parameters in Lisp. zaxonus Programming 2 11-05-2011 09:27 AM
[SOLVED] echo function in .bashrc supulton Linux - Newbie 3 02-25-2010 10:34 PM
bash function parameters ArthurHuang Programming 1 05-18-2009 07:01 PM
function in .bashrc ovince Programming 6 04-25-2007 01:26 PM


All times are GMT -5. The time now is 01:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration