How to Get the File Input from Parameter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Get the File Input from Parameter
# 1  
Old 06-30-2009
How to Get the File Input from Parameter

Code:
pdir=`pwd`
if [ $# -lt 1 ]; then
 echo current directory $pdir
 ls -altr
 echo
fi
for f in $*
do
    # directory
    if [ -d $f ]; then
      echo current directory $f
      cd $f
      ls -latr
      echo
    fi

    # but you can test file/dir
    # regular file only
    if [ -f $f ]; then
      echo current directory $pdir
      ls -altr $f
      echo
    fi

done

i have a basic code on shell script that accepts parameter for directory name with or without a filename or extension file name.

if i enter this below on the command line
Code:
sh dir_file_list.sh /u02/app/eatv/dev/out/*.txt

it give me this output:
Code:
current directory /export/home/ccalftdv/apps
-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 09:30 /u02/app/eatv/dev/out/
sample.txt

$

how do i capture the string *.txt or the filename on the input parameter? thanks.
# 2  
Old 06-30-2009
If i have got your question right, to capture the filename, use the 'basename' command.

#basename /u02/app/eatv/dev/out/sample.txt
sample.txt
#
# 3  
Old 06-30-2009
Code:
#!/bin/sh
pdir=`pwd`
if [ $# -lt 1 ]; then
 echo current directory $pdir
 ls -altr
 echo
fi
for f in $*
do
    # directory
    if [ -d $f ]; then
      echo current directory $f
      cd $f
      ls -latr
      echo
    fi

    # but you can test file/dir
    # regular file only
    if [ -f $f ]; then
      pfile=`basename $f`
      echo current directory $pdir
      ls -altr $pfile
      echo
    fi

done

thanks. i added the basename command and i was getting this error:
Code:
$ sh dir_file_list.sh /u02/app/eatv/dev/out/*.txt
current directory /export/home/ccalftdv/apps
ls: 0653-341 The file sample.txt does not exist.

$

# 4  
Old 07-01-2009
Before you have the line "ls -altr $f", you need to run the 'cd' command to go to the current directory pdir.
# 5  
Old 07-01-2009
Code:
#!/bin/sh
pdir=`pwd`
if [ $# -lt 1 ]; then
 echo current directory $pdir
 ls -altr
 echo
fi
for f in $*
do
    # directory
    if [ -d $f ]; then
      echo current directory $f
      cd $f
      ls -latr
      echo
    fi

    # but you can test file/dir
    # regular file only
    if [ -f $f ]; then
      pfile=`basename $f`
      pdir=`dirname $f`
      echo current directory $pdir
      cd $pdir
      ls -altr $pfile
      echo
    fi

done

thanks that works when i added the change directory cd command. lastly is to fix the display. when i run it gives me this directory listing where the display for the current directory was repeated.

Code:
$ sh dir_file_list.sh /u02/app/eatv/dev/out/*.txt
current directory /u02/app/eatv/dev/out
-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 09:30 sample.txt

current directory /u02/app/eatv/dev/out
-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 17:05 sample2.txt

current directory /u02/app/eatv/dev/out
-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 17:05 sample3.txt

$

it should look like this:
Code:
current directory /u02/app/eatv/dev/out
-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 09:30 sample.txt
-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 17:05 sample2.txt
-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 17:05 sample3.txt



---------- Post updated at 01:09 PM ---------- Previous update was at 12:38 PM ----------

Code:
pdir=`pwd`

if [ -f $# ]; then
  pfile=`basename $#`
  pdir=`dirname $#`
fi

if [ -d $# ]; then
  pdir=`$#`
fi

echo current directory $pdir

if [ $# -lt 1 ]; then
 ls -altr
 echo
fi


for f in $*
do
    # directory
    if [ -d $f ]; then
      cd $f
      ls -latr
      echo
    fi

    # but you can test file/dir
    # regular file only
    if [ -f $f ]; then
      pfile=`basename $f`
      pdir=`dirname $f`
      cd $pdir
      ls -altr $pfile
    fi

done

i modified the script a bit and i was able to get the expected results.

Code:
$ sh dir_file_list.sh
current directory /export/home/ccalftdv/apps
total 152
drwxr-sr-x    3 ccalftdv ccalogrp        512 Sep 13 2006  dev
-rwxr-xr-x    1 ccalftdv ccalogrp       5586 Jun 08 15:55 cca_monthly_load_file
ksh
-rwxr--r--    1 ccalftdv ccalogrp       5729 Jun 08 15:55 load_file.ksh
-rwxrwxrwx    1 ccalftdv ccalogrp         63 Jun 11 15:14 display_date.sh
-rwxr-xr-x    1 ccalftdv ccalogrp        680 Jun 25 08:50 cca_monthly_load_data
ini
-rw-r--r--    1 ccalftdv ccalogrp        679 Jun 25 10:28 load_data.ini
drwxr-sr-x    4 ccalftdv ccalogrp        512 Jun 29 13:53 ..
-rw-r-----    1 ccalftdv ccalogrp       1298 Jun 30 08:26 dir_list_ccalloc_in.s
-rw-r-----    1 ccalftdv ccalogrp       1298 Jun 30 08:26 dir_list_ccalloc_out.
h
-rw-r-----    1 ccalftdv ccalogrp        166 Jun 30 09:32 sample.sh
-rw-r-----    1 ccalftdv ccalogrp       1357 Jun 30 09:39 dir_list_eatv_out.sh
-rw-r--r--    1 ccalftdv ccalogrp        385 Jun 30 10:49 sample2.sh
-rw-r--r--    1 ccalftdv ccalogrp        375 Jun 30 11:13 sample3.sh
-rw-r--r--    1 ccalftdv ccalogrp          9 Jun 30 17:04 sample2.txtw
-rw-r--r--    1 ccalftdv ccalogrp        454 Jul 01 12:54 dir_file_list.sh.bak
drwxr-sr-x    3 ccalftdv ccalogrp        512 Jul 01 12:54 .
-rw-r--r--    1 ccalftdv ccalogrp       1595 Jul 01 12:58 dir_file_list.sh

$ sh dir_file_list.sh /u02/app/eatv/dev/out
current directory /export/home/ccalftdv/apps
total 32
drwxrwxr-x    4 oracle   eatvgrp         256 Jun 19 09:41 ..
-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 09:30 sample.csv
-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 09:30 sample.txt
-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 17:05 sample2.txt
-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 17:05 sample3.txt
drwxrwxr-x    2 oracle   eatvgrp         256 Jun 30 17:05 .

$ sh dir_file_list.sh /u02/app/eatv/dev/out/*.csv
current directory /export/home/ccalftdv/apps
-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 09:30 sample.csv
$ sh dir_file_list.sh /u02/app/eatv/dev/out/*.txt
current directory /export/home/ccalftdv/apps
-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 09:30 sample.txt
-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 17:05 sample2.txt
-rw-r--r--    1 ccalftdv ccalogrp          7 Jun 30 17:05 sample3.txt
$



---------- Post updated at 03:38 PM ---------- Previous update was at 01:09 PM ----------

Code:
pdir=`pwd`

if [ $# -lt 1 ]; then
  echo current directory $pdir
  ls -latr
  echo
else
  p1=$1
  if [ -d $p1 ]; then
    pdir=$p1
    echo current directory $pdir
    cd $pdir
    ls -latr
    echo
  elif [ -f $p1 ]; then
    pdir=`dirname $p1`
    echo current directory $pdir
    cd $pdir

    for f in $*
    do
      pfile=`basename $f`
      ls -altr $pfile
    done
  else
    echo $p1 not found
  fi
fi

# put a white space

i revised the code a bit.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace value from input parameter

Hi Guys, I am having a script file where in getting input parameter as string. I need to assign the variable but not able to achieve #!/bin/bash input=$1 replace=string_$input_string2 echo $replace I am getting but should get string_<input_value>_string2 string_ (1 Reply)
Discussion started by: rohit_shinez
1 Replies

2. UNIX for Beginners Questions & Answers

Need list of input and output parameter of task in a text file, using shell script

//file begin ===== //some code task abcd_; input x; input y,z; //some comment output w; //some comment reg p; integer q; begin //some code end endtask : abcd_ //some code //file end ===== expected output from above... (1 Reply)
Discussion started by: rishifrnds
1 Replies

3. Shell Programming and Scripting

How pass the input parameter to a file in the script ?

OS version: RHEL 6.7 myTextFile.txt file is referred within Script1.sh script, I only execute Script1.sh and I want the input variable to be passed inside myTextFile.txt . Any idea how I can do this ? $ cat script1.sh cat myTextFile.txt $ cat myTextFile.txt $1 Requirement1.... (4 Replies)
Discussion started by: kraljic
4 Replies

4. Shell Programming and Scripting

Retaining file versions based on input parameter

Hello Forum. I have the following files in one directory: abc_july01_2013.txt abc_july02_2013.txt abc_july03_2013.txt abc_july04_2013.txt abc_july05_2013.txt abc_july06_2013.txt abc_july07_2013.txt abc_july08_2013.txt If I want to be able to keep the last 5 versions of the file and... (4 Replies)
Discussion started by: pchang
4 Replies

5. Shell Programming and Scripting

Check input parameter

Hi all i need to check that if user has passed any input parameter while executing he shell script like ./test1.sh -a"-v" then do smothing if user execute the script without giving input paramater then ./test1.sh then do something how can we check this input parameter (6 Replies)
Discussion started by: aishsimplesweet
6 Replies

6. UNIX for Dummies Questions & Answers

Reading from a file(passing the file as input parameter)

hi I have a shell script say primary.sh . There is a file called params my scenario is primary.sh should read all the values and echo it for example i should pass like $primary.sh params output would be Abc ... (2 Replies)
Discussion started by: ssuresh1999
2 Replies

7. Shell Programming and Scripting

Pass input and output file as parameter to awk script

Hi, i am new to awk. I am using csv2pipe script(shown below) BEGIN { FS=SUBSEP; OFS="|" } { result = setcsv($0, ",") print } # setcsv(str, sep) - parse CSV (MS specification) input # str, the string to be parsed. (Most likely $0.) # sep, the separator between the values. # #... (6 Replies)
Discussion started by: bhaskarjha178
6 Replies

8. Shell Programming and Scripting

awk/input parameter

Hi, My script takes in one input parameter($1-email id) on the command line... The script contains something like this... awk '$1 == 400' abc.log >def.log mail -s subject $1 <def.log abc.log looks something like this... 300 222 330 123 445 400 098 890 727 663 How do i make the... (3 Replies)
Discussion started by: wannalearn
3 Replies

9. Shell Programming and Scripting

Input parameter format

Hi, My script expects 2 inputs and one of them is supposed to be time, I would like to check if the given input is in validate format (i.e. 16:00), could anyone help me out here? thanks! (1 Reply)
Discussion started by: mpang_
1 Replies

10. UNIX for Dummies Questions & Answers

Shell script with input parameter

Can anyone help me how to write a shell script which accepts input parameter. My requirement is as follows: I need to run a shell script with a input parameter, and inside the script i will create a file with this input parameter name. Please help me out to create such a shell script. ... (1 Reply)
Discussion started by: jhmr7
1 Replies
Login or Register to Ask a Question