Invoking commands in "parent" environment


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Invoking commands in "parent" environment
# 1  
Old 02-28-2009
Invoking commands in "parent" environment

The below shell script obtains a command and an alias from the user then appends the alias to the users ~/.aliases file. It is useful with cd commands and saves typing; instead of going cd /foo/bar/bonk/boo/what/whatelse/dir I create an alias something like "godir".

It's not done and I will post it on this thread when it is. If you want to use it, add "source ~/.aliases" to ~/.bashrc, if it's not there. I hereby release it to the public domain.

Code:
#!/bin/bash
#mka -- make an alias, append it to ~/.aliases, source ~/.aliases and make the new alias available to the parent environment.

_dir=$HOME
_aliasfile="$_dir/.aliases"
[ -f $_aliasfile ] || echo "#! /bin/bash" > $_aliasfile
# so it looks pretty in vi

echo -n "Enter command: "
read _command

echo -n "Enter alias name: "
read _aliasname

_entry="alias $_aliasname='$_command'"
echo "The following entry will be made to your ~/.aliases file:"
echo
echo "---$_entry---"
echo
echo "Append entry to ~/.aliases?"
read _ans
case "$_ans" in
    y)
    echo $_entry >> $_aliasfile
    echo
    echo "Here are your ~/.aliases file contents:"
    echo
    cat $_aliasfile
    echo
    echo "Sourcing $_aliasfile..."
    source $_aliasfile
    if [ $? = 0 ]; then
        echo "Done."
    else
        echo "Error."
    fi
    ;;
    n)
    echo "Alrighty then."
    ;;
esac

My problem is I'd like to use the alias as soon as the script exits, but

Code:
source $_aliasfile

doesn't affect the environment of the shell running in the terminal window within which the script is invoked. How would this be done?

Eternal gratitude for the being who helps me solve this puzzle.
# 2  
Old 02-28-2009
Have you source this (main) script?

Regards
# 3  
Old 02-28-2009

You cannot change anything in the parent environment. If you want a script to change the calling environment, it must be sourced, not executed. (Or put it in a function.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Beginners Questions & Answers

Extract delta records using with "comm" and "sort" commands combination

Hi All, I have 2 pipe delimited files viz., file_old and file_new. I'm trying to compare these 2 files, and extract all the different rows between them into a new_file. comm -3 < sort file_old < sort file_new > new_file I am getting the below error: -ksh: sort: cannot open But if I do... (7 Replies)
Discussion started by: njny
7 Replies

3. Red Hat

files having Script which works behind "who" & "w" commands

Dear All, plz print the path of files which have the script of "who" & "w" commands. thnx in advance. (6 Replies)
Discussion started by: saqlain.bashir
6 Replies

4. Solaris

Relation btw commands, "man" and "more" ???

Hi guys, Hope u r doing find. I have this query. When we check the manual pages for a certain command, say man cat we see the manual page with more What is UNIX really doing here, I mean why not less command instead of more command. And can we have UNIX display the manual pages with less command... (2 Replies)
Discussion started by: gabam
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

Breaking "while read" also breaks the parent process

Hi, I'm a bit confused. Maybe some master can explain to me what is happening. I have a program that starts issuing output about himself loading. I want to run it in another thread but I want to wait untill it's fully loaded. Program sample: $ cat myprogram echo "loading" echo "almost... (3 Replies)
Discussion started by: chebarbudo
3 Replies

7. OS X (Apple)

Neither "which" nor "find" commands work

hi there, could use some basic PATH advice, i think, or something find sometimes work, but which hasn't ever seemed to. for years! what am i doing wrong that the commands which and find rarely work? they used to work on the workstations i used ages ago... running 10.5.8 because i... (4 Replies)
Discussion started by: zensnob
4 Replies

8. Shell Programming and Scripting

Unix commands delete all files starting with "X" except "X" itself. HELP!!!!?

im a new student in programming and im stuck on this question so please please HELP ME. thanks. the question is this: enter a command to delete all files that have filenames starting with labtest, except labtest itself (delete all files startign with 'labtest' followed by one or more... (2 Replies)
Discussion started by: soccerball
2 Replies

9. UNIX for Dummies Questions & Answers

"nohup" and "&" commands

Why would anyone ever type in a command like this: nohup command & nohup lets you logout of your telnet session so why add "&" to run it in the background? (1 Reply)
Discussion started by: xadamz23
1 Replies

10. UNIX for Advanced & Expert Users

Commands on Digital Unix equivalent to for "top" and "sar" on other Unix flavour

Hi, We have a DEC Alpha 4100 Server with OSF1 Digital Unix 4.0. Can any one tell me, if there are any commands on this Unix which are equivalent to "top" and "sar" on HP-UX or Sun Solaris ? I am particularly interested in knowing the CPU Load, what process is running on which CPU, etc. ... (1 Reply)
Discussion started by: sameerdes
1 Replies
Login or Register to Ask a Question