Sponsored Content
Top Forums Shell Programming and Scripting A simple query on unix shell script Post 302160951 by strobotta on Wednesday 23rd of January 2008 07:40:12 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
script(1)							   User Commands							 script(1)

NAME
script - make record of a terminal session SYNOPSIS
script [-a] [filename] DESCRIPTION
The script utility makes a record of everything printed on your screen. The record is written to filename. If no file name is given, the record is saved in the file typescript. See WARNINGS. The script command forks and creates a sub-shell, according to the value of $SHELL, and records the text from this session. The script ends when the forked shell exits or when Control-d is typed. OPTIONS
The following option is supported: -a Appends the session record to filename, rather than overwriting it. NOTES
script places everything that appears on the screen in filename, including prompts. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ |CSI |Enabled | +-----------------------------+-----------------------------+ SEE ALSO
attributes(5) WARNINGS
script can pose a security risk when used in directories that are writable by other users (for example, /tmp), especially when run by a privileged user, that is, root. Be sure that typescript is not a link before running script. SunOS 5.11 30 Jan 2004 script(1)
All times are GMT -4. The time now is 04:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy