Solved

shell script to loop and ask for a new filename

Posted on 2006-07-12
6
3,070 Views
Last Modified: 2013-11-17
The script  loops okay and ask for a filename
but I cant get the variable "file' to increment
thanks in advance
Jeff
#! /usr/bin/ksh
while true; do
echo "enter your files "
read file
echo $file
file=file+1
    echo "Would you like to continue? \c"
read ans
case $ans in
    [yY] | [yY][eE][sS])  echo "Fine, then we'll continue."
    #`file=file+1`
    #break
    ;;
    [nN] | [nN][oO]) echo "We shall now stop."
break
   ;;
   *) echo "You must enter a yes or no !"
esac
done
echo "\nWe are now out of the while loop."
echo "-----------------------------------------------------------"
0
Comment
Question by:urbanweasel
6 Comments
 
LVL 14

Expert Comment

by:sjm_ee
ID: 17094309
Hmm

Your script says:

read file
echo $file

So file is a string, yes? So why then do you try to increment a string?
0
 
LVL 1

Author Comment

by:urbanweasel
ID: 17096047
so I can add in addtional file names
0
 
LVL 1

Author Comment

by:urbanweasel
ID: 17098822
My issue is that I need to add 1 or more It could be as many as 8 files
I cant figure out a way to do this with out the loop
I had a script that did it with hitting the enter key
to go past the uneeded file requests but auditing
kicked that one back.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 6

Expert Comment

by:JJSmith
ID: 17111763

ans="y"
while [ $ans != "n" ]
do
   echo "Enter filename - Return to finish \c"
   read file
   if [ "$file" = "" ]; then
      break                              # if no file then break out
   else
      flist=$flist:$file                 # add the last file name to a list - seperater by a colon:
      echo "Continue Y/N?\c"     # ask to continue
      read ans
      ans=`echo $ans | tr "[:upper:]" "[:lower:]" | cut -c1`    # force the initial letter to lowercase
   fi
done

# I don't know what you intend to do with these file names so I'll simply echo them

if [ ! -s "$flist" ]; then
   echo "no files to process"
   exit
fi

# Unload file list

set `echo $flist | sed s!:! !`      # put all file names into environment vars
while [ $# -ne 0 ]
do
   echo $1
   shift
done


Now I can't promise this will work as I have no UNIX here - but it should be a good starter for 10.

Cheers
JJ
   
   
   
0
 
LVL 1

Expert Comment

by:bengtbj
ID: 17136600
Tested JJSmith's script, did not work on my machine, possibly because I tried to use ksh to run it, also, not to hot on sed, so I changed the way it stores the variables.
I have changed the following (and I am certain the original might work using another script language then ksh)

Line 1, what interpreter the script should use, in my case /bin/ksh
Line 10, changed it from colon delimited to space delimited, because I am lazy, the script will probabbly not work with filenames containing spaces, but who wants them anyway. I did this because I do not like sed, or well, am not perticularly hot on it.
Line 19, change the [ ! -s "$flist"] expression to [ "" == "$flist" ], because I am lazy and the fact that I have never managed to get -s to work the way I want.
Line 26,27 removed the sed statement, and change the hile for a for loop, and as dear mister Korn allowed for to use space as deleimiter, I have to care .... about how the list is delimited. This because I am lazy :)



#!/bin/ksh
ans="y"
while [ $ans != "n" ]
do
   echo "Enter filename - Return to finish \c"
   read file
   if [ "$file" = "" ]; then
      break                              # if no file then break out
   else
      flist="$flist $file"                 # add the last file name to a list - seperater by a colon:
      echo "Continue Y/N?\c"     # ask to continue
      read ans
      ans=`echo $ans | tr "[:upper:]" "[:lower:]" | cut -c1`    # force the initial letter to lowercase
   fi
done

# I don't know what you intend to do with these file names so I'll simply echo them

if [ "" == "$flist" ]; then
   echo "no files to process"
   exit
fi

# Unload file list

#`echo $flist | sed s!:! !`      # put all file names into environment vars
for i in `echo $flist`
do
   echo "You enterd: $i"
done
0
 
LVL 6

Accepted Solution

by:
JJSmith earned 250 total points
ID: 17148557

I did say it was a starter for 10 - because I have no unix here and I'm afraid that is still the case.

But one thing I did give you that is definately wrong is:

     if [ ! -s "$flist" ]; then

it should be a -z to test a string, not -s which tests a file - sorry.

but it looks like you are almost there anyhow.

Cheers
JJ
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question