How do I import variables and functions defined in lib.ksh script to another ksh script called setup.ksh under Linux / Unix like operating systems? How do I execute commands from a file in the current KSH shell?
To read and execute commands from given FILENAME in the current shell use the following syntax:
. /path/to/file
OR
. /path/to/lib.ksh
The . (dot) is one of the KSH built-in commands. You can also use an alias called source as follows:
source /path/to/lib.ksh
Example
Create a script called /tmp/lib.ksh as follows:
#!/bin/ksh # defaul values ## vech="Bus" rent=14 type="A/C" # Display info function showvech { printf "Vehicle: %s\n" $vech printf "Type (ac or non-ac): %s\n" $type printf "Rent (per/km): Rs.%d km\n" $rent } # Set info function setvech { vech="$1" rent=$2 type="$3" }
Create a script called test.ksh:
#!/bin/ksh # source our /tmp/lib.ksh . /tmp/lib.ksh # show defaults showvech # set new values and display it back setvech "Jeep" 9 "Non-A/C" showvech
Sample outputs:
Vehicle: Bus Type (ac or non-ac): A/C Rent (per/km): Rs.14 km Vehicle: Jeep Type (ac or non-ac): Non-A/C Rent (per/km): Rs.9 km
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop









![Linux: HowTo Copy a Folder [ Command Line Option ]](http://s0.cyberciti.org/images/rp/1/28.jpg)

{ 0 comments… add one now }