LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-05-2006, 01:08 PM   #1
Ictus
Member
 
Registered: Jan 2005
Location: Germany
Distribution: Fedora Core 4
Posts: 32

Rep: Reputation: 15
own cd command


Hi!

I've written a script "netmount" which can easily mount and manage the network ressources of my local area network.

Till now i'm doing it this way:

# netmount Music
# cd /mnt/192.168.1.13/Music

I'd rather like to do it like this:

# netmount Music --cd

That if the parameter --cd is passed the netmount command changes
directory automatically.
How can i achieve this? If i use "cd" within the netmount-script it has no effect on my actual shell.
 
Old 03-05-2006, 01:34 PM   #2
Brian1
LQ Guru
 
Registered: Jan 2003
Location: Seymour, Indiana
Distribution: Distribution: RHEL 5 with Pieces of this and that. Kernel 2.6.23.1, KDE 3.5.8 and KDE 4.0 beta, Plu
Posts: 5,700

Rep: Reputation: 65
Post contents on netmount script?
 
Old 03-06-2006, 05:49 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
you can 'dot' the script, though an 'exit' will kill your shell
or
you can make it a function in your current shell
like
Code:
netmount()
{
   blah
   blah
   cd
}
 
Old 03-06-2006, 09:15 AM   #4
Ictus
Member
 
Registered: Jan 2005
Location: Germany
Distribution: Fedora Core 4
Posts: 32

Original Poster
Rep: Reputation: 15
The netmount script currently looks like this:
Code:
#!/usr/bin/perl
 
# mountet Freigaben aus Windows-Netzwerken
#
# Usage: netmount [Optionen] [Freigabe]
#
# Standard für IP ist 192.168.1.13 (für MEIN Netzwerk)


$IP = "192.168.1.13";
$freigabe = "";
$mount = 1; # 1 = mounten; 0 = unmounten

if(scalar(@ARGV) < 1)
{
 print "missing or wrong argument\n";
}
else
{

 # Argumente durchlaufen
 for($i=0;$i<scalar(@ARGV);$i++)
 {
  $param = $ARGV[$i];
 
  # OPTIONEN VERARBEITEN
  if(substr($param,0,1) eq "-")
  {
   # -i : IP angeben
   if($param eq "-i") { $IP = $ARGV[$i+1]; $i += 1; }
   elsif($param eq "-u") { $mount = 0; } # unmounten
  }
  else
  {
   # Freigabe
   $freigabe = $param;
  }
 }

 if($freigabe eq "")
 {
  print "nothing to mount.\n";
  exit;
 }
 
 $host_dir = "/mnt/$IP";
 $dir = "/mnt/$IP/$freigabe";

 # Hauptverzeichnis für Freigaben dieser IP erstellen
 unless( -d $host_dir)
 {
  print "creating host dir...";
  if(mkdir("/mnt/$IP", 0755)) {
    print "ok\n";
  }
  else {
    print "fail\n";
  }
 } 

 # Verzeichnis für Freigabe erstellen
 unless( -d $dir)
 {
  print "creating mount dir...";
  if(mkdir($dir, 0755)) {
    print "ok\n";
  }
  else {
    print "fail\n";
  }
 }

 if($mount == 1) # mounten
 {
  print "mounting $freigabe at $dir...\n";
  print `sudo mount -t cifs //$IP/$freigabe $dir`;
 }
 else # unmounten und Verzeichnisse wieder löschen
 {
  print "unmounting $freigabe...\n";
  print `sudo umount $freigabe`;
  print "deleting $dir...";
  if(rmdir($dir)) { print "ok\n"; }
  else { print "fail\n"; }
 }
}
The problem is, that it is a Perl script. I guess i can't use Perl in a bash function. :-\
 
Old 03-06-2006, 09:22 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
aaah! hmm!
well...
 
Old 03-06-2006, 11:23 AM   #6
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
You could use an alias as well as the script:
alias somecommand='real_command.pl $@;cd $1'

You may want to return a specific exit code from real_command.pl and use it to check if you need to run the cd command.
 
Old 03-06-2006, 01:06 PM   #7
Ictus
Member
 
Registered: Jan 2005
Location: Germany
Distribution: Fedora Core 4
Posts: 32

Original Poster
Rep: Reputation: 15
Thanks for your help!
I finally made it. )

I changed the netmount script so that it only echos the direction where the ressource was mounted, on success. Then i created that tiny function in my .bashrc file:
Code:
function xmount()
{
 DIR=`netmount $1`;
 cd $DIR;
}
Amazingly easy. lol

Thanks again, u gave me the necessary ideas to achieve my goal.
 
Old 03-06-2006, 05:51 PM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I would have never thought of that. Could you also have changed PWD and exported it?
ta0kira
 
Old 03-08-2006, 10:47 AM   #9
Ictus
Member
 
Registered: Jan 2005
Location: Germany
Distribution: Fedora Core 4
Posts: 32

Original Poster
Rep: Reputation: 15
Sorry, i dont really understand what you mean. Which password, and exporting what?
 
Old 03-08-2006, 12:17 PM   #10
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I mean the PWD bash environmental variable; it stores the current path. Type in 'env | grep $PWD' in bash and it should show your current path. I was just talking about changing that and exporting it from the script. I don't know how that would affect other open shells, however.
ta0kira
 
  


Reply



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
Is there a single command to list all hardware installed (command line)? davee Linux - Hardware 6 02-28-2009 07:19 PM
Require Linux/Perl equivalent command for windows Command alix123 Programming 7 08-19-2005 02:23 AM
Key stroke/command to shut down x and go into the command prompt screen? Fear58 Linux - General 1 07-14-2004 07:14 PM
Command to display whole filestructure hierarchy f/ command line? mjewell Linux - Newbie 10 01-19-2004 10:48 AM
Where is Command line utility for Cups and command tutorial mossy Linux - Software 8 01-16-2004 12:24 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:37 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
Open Source Consulting | Domain Registration