A simple query on unix shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A simple query on unix shell script
# 1  
Old 01-23-2008
A simple query on unix shell script

I want to write a script to go to particular path in file and run shell script from there.


what will be shell script for the same.
# 2  
Old 01-23-2008
possible solution

a possible solution could be:
Code:
#!/bin/bash

if [ -z $1 ]; then
  exit;
fi

current_directory=`pwd`

# 2nd call of the script
if [ "$1" == "$current_directory" ]; then
  ls -l
# first call of the script
else
  cd $1 2>/dev/null
  if [ $? -ne 0 ]; then
    echo "no such directory"
  else
    if [ "${0:0:1}" == "/" ]; then
      $0 `pwd`
    else
      $current_directory/$0 `pwd`
    fi
  fi
fi

This script needs to be called with one argument. That must be a directory name. The script will then change to that directory and do a simple ls -l.

1st check if there is one argument submited (if not quit)
2nd remember the current working directory
3rd check if current woring directory and the target directory (submited in $1) is identical.
- If thats the case, just do an ls -l and then quit.
- If this is not the case, change to the target directory. Check if cd was successfull and then run this script ($0) again with now the current directory as the argument.

The script itself resides still in the old directory (or somewhere else). Therefore a script call with a relative path would fail. So the old path before changing to the target directory must be submited as well to locate the script. If the script was called with an absolute path, $0 can be used without changes (will be located anyway).
You can ommit this problem if you put your script into your $PATH.
# 3  
Old 01-23-2008
Some modification for the given script to handle spaces in paths, etc.:

#!/bin/ksh
[[ -z "${1}" ]] && { print "Please specify a path";return 1; }
[[ -d "${1}" ]] && { print "Specified path is a directory! You should specify a path to executable.";return 1; }
[[ -x "${1}" ]] || { print "Specified path is not executable!";return 1; }
typeset OldPwd=$(pwd)
typeset NewPwd="${1%/*}"
typeset ExecName="${1##/}"
typeset -i RC=0
# The file could be located in current directory, ex. `script.ksh script.ksh`
[[ -n "${NewPwd}" ]] && cd "${NewPwd}"
"${ExecName}"
RC=$?
print -- "Exited with status ${RC}"
# Usually `cd -` would work as well
cd "${OldPwd}"
return ${RC}


Note, that is probably not so important for you:
You should expect some problems with "current directory" if the file is a link. To obtain real directory that contain the executable, and not the link, you should do some more things. But this is probably not what you would like to do.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Interactive UNIX shell query

Hi guys, I've create 3 shells concerning my work, which named as 1.sh, 2.sh and 3.sh. However, how can I make an interactive query for these shells just like the old (fdisk) in windows9x. I want to make an interface tells the user just like this: Press 1 to execute "1.sh" Press 2 to... (5 Replies)
Discussion started by: leo_ultra_leo
5 Replies

2. Shell Programming and Scripting

Shell Script to execute Oracle query taking input from a file to form query

Hi, I need to query Oracle database for 100 users. I have these 100 users in a file. I need a shell script which would read this User file (one user at a time) & query database. For instance: USER CITY --------- ---------- A CITY_A B CITY_B C ... (2 Replies)
Discussion started by: DevendraG
2 Replies

3. Shell Programming and Scripting

Unix shell script to query linux top consuming processes

Hi All, O/S: Linux 86x64 Red Hat I have a sql script that queries top consuming processes of Linux using TOP commnd. Now I need to automate this task and pass the top processes i.e., PID to the sql script through unix shell script. Could anyone please let me know how to achieve this. ... (2 Replies)
Discussion started by: a1_win
2 Replies

4. Shell Programming and Scripting

Isql query in unix shell

Hi i want write a script for list of sysbase are having access or open. then i wrote like: USER="abc" PASS="xyz" SERVER="SCCS" DB="blue" WORK_DIR="/usr/home/ramakrishna" set -x isql -U${USER} -P${PASS} -S${SERVER}<<EOF>$WORK_DIR/output.log go use blue (database name) go use... (0 Replies)
Discussion started by: koti_rama
0 Replies

5. Shell Programming and Scripting

Query Oracle tables and return values to shell script that calls the query

Hi, I have a requirement as below which needs to be done viz UNIX shell script (1) I have to connect to an Oracle database (2) Exexute "SELECT field_status from table 1" query on one of the tables. (3) Based on the result that I get from point (2), I have to update another table in the... (6 Replies)
Discussion started by: balaeswari
6 Replies

6. UNIX for Dummies Questions & Answers

executing SQL query using unix shell script

I want to perform few post-session success tasks like update a status to 'true' in one of the sql database table, update date values to current system date in one of the configuration table in sql. How do i achieve this in a post session command?syntax with example will be helpful. (3 Replies)
Discussion started by: nathanvaithi
3 Replies

7. Shell Programming and Scripting

simple shell - how to get a parameter typed in a shell script

Hi, I am new to unix and using linux 7.2. I would like to create a script that would make it easyer for me to run my java programms. At the moment I have to type java myJavaprogram I am trying to write a script that will allow me to type something like this "myscript myJavaprogram" or maybe... (4 Replies)
Discussion started by: cmitulescu
4 Replies

8. Shell Programming and Scripting

isql query in unix shell script

Dear all I want to execute some isql command from unix shell script. Kindly suggest me. isql command mention below. isql -U -P use gdb_1 go select count (*) from table_x go (3 Replies)
Discussion started by: jaydeep_sadaria
3 Replies

9. UNIX for Dummies Questions & Answers

sql query results in unix shell script

Hi I want to get the a field from a SQL query into unix shell script variable. the whole situation is like this. 1. Opened a cursor to a table in DB2 databse. 2. Fetching individual rows with the help of cursor. 3. Each row has 4 fields. I want each of the field in individual shell... (1 Reply)
Discussion started by: skyineyes
1 Replies

10. UNIX for Dummies Questions & Answers

Simple UNIX Shell Script help, PLEASE

I don't know anything about UNIX. I have been developing on NT platform and now a batch file I was running in my java code must work on UNIX instead. How do I change the below .bat file into a shell script that can be run on UNIX? Thanks in advance for your help. @ECHO OFF D:... (1 Reply)
Discussion started by: ci2a020
1 Replies
Login or Register to Ask a Question