Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

shell script to loop and ask for a new filename

Posted on 2006-07-12
6
Medium Priority
?
3,076 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
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…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

610 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