Help to cd to a dirctory and not exit after cmd


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help to cd to a dirctory and not exit after cmd
# 1  
Old 05-28-2015
Help to cd to a dirctory and not exit after cmd

Code:
cmd()
{
    results=`eval $* 2>&1`
    val=$?  #return the $* execution value 
}

Code:
cmd "cd /home/tom || exit 0"

how to realize two point
  1. it cd to "/home/tom"
  2. not exit from the script after the ----> cmd cd /home/tom || exit 0

Last edited by rbatte1; 05-28-2015 at 08:55 AM.. Reason: Converted to LIST=1 tags from plain text and added ICODE tags
# 2  
Old 05-28-2015
It is very simple. If you don't want to exit; don't issue an exit command; or be sure that the file /home/tom exists, is a file of type directory, and that the user invoking cmd has permission to cd into that directory.
# 3  
Old 05-28-2015
Quote:
Originally Posted by Don Cragun
It is very simple. If you don't want to exit; don't issue an exit command; or be sure that the file /home/tom exists, is a file of type directory, and that the user invoking cmd has permission to cd into that directory.
That's not what I wanted, I just give the example that I need to exit 0 intentionally, can you help me to re-write the cmd function
to achieve this ?
# 4  
Old 05-28-2015
I'm not sure what you want to achieve. The command substitution in backticks will run in a subshell, so this subshell will be exited and wont harm neither the function nor the main script. Surprisingly the exit code will be assigned to val. Any "command string" will be valid for the subshell only, so even if the cd succeeded, it will be lost for both function and script.
# 5  
Old 05-28-2015
I've found the command exec sh useful for situations when you need to switch the shell from running script commands to running interactively in the same context.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Share Dirctory in Solaris

Hi, I am trying to share a directory from a Solaris 10 m/c to another Solaris 10 m/c.But I am getting an error .Please let me know if I have missed anything On Server 1 svcsadm -v enable -r network/nfs/server share -F nfs -o rw /mount_point In /etc/dfs/dfstab ------ share -F nfs -o rw... (5 Replies)
Discussion started by: Rossdba
5 Replies

2. Shell Programming and Scripting

Test exit status of last cmd via ssh

see below for a housekeeping script which constructs an ssh cmd using some server/path/sudo info found in $HRINST. the script should hop to each server and if it finds a file to cleanup, moves it to the archive dir if there is nothing to move, it should report so and email the output ... (3 Replies)
Discussion started by: jack.bauer
3 Replies

3. Shell Programming and Scripting

Perl open(CMD, "cmd |"); buffering problem..

Hello, There's a third-party application's command that shows the application's status like "tail -f verybusy.log". When use the command, the output comes every 1-sec. but when it goes in a script below the output comes every 8-sec...What is the problem and how can I fix it? open(CMD,... (2 Replies)
Discussion started by: Shawn, Lee
2 Replies

4. Shell Programming and Scripting

cmd || echo "something" - doesn't exit uppon error

Hi guys, I have a shell script where I have the following: for i in ad0 ad1 do gpart create -s gpt $i || echo "Cannot create GPT partition on "$i". Exiting ..." gpart add -s 128 -t freebsd-boot $i || echo "Cannot add freebsd-boot partition on "$i". Exiting ..." gpart add -s 4G -t... (2 Replies)
Discussion started by: da1
2 Replies

5. Shell Programming and Scripting

Unix cmd prompt how to get old cmd run?

Hi, I am using SunOS I want to serch my previous command from unix prompt (like on AIX we can search by ESC -k) how to get in SunOs urgent help require. (10 Replies)
Discussion started by: RahulJoshi
10 Replies

6. Filesystems, Disks and Memory

How to find the total size of a dirctory tree in Solaris

Hi, I want to find the total size of some directory trees in my solaris 9 machine. Is there a command or utility I can use to do it. Please let me know if there is any way. Thanks Akheel (1 Reply)
Discussion started by: 0ktalmagik
1 Replies

7. Solaris

Remove non empty dirctory

Hi, Any command or means to delete a director which is not empty rmdir or similar iam using Sun Solaries 2.6 :confused: I have many full directories with subdirectories and I can not go on emptying them all (5 Replies)
Discussion started by: adol3
5 Replies

8. UNIX for Dummies Questions & Answers

man <cmd> >> cmd.txt

I've noticed most of my postings here are because of syntax errors. So I want to begin compiling a large txt file that contains all the "man <cmd>" of the commands I most have problems with. I ran a "man nawk >> nawk.txt" but it included a header/footer on each "page". Anyone know how I'd be... (6 Replies)
Discussion started by: yongho
6 Replies

9. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies

10. UNIX for Dummies Questions & Answers

FTP a Dirctory

I am easily able to ftp a file with: get <file> but how do I ftp a directory and all the files in it? Thanks (2 Replies)
Discussion started by: radimus
2 Replies
Login or Register to Ask a Question