?
Solved

shell script to loop and ask for a new filename

Posted on 2006-07-12
6
Medium Priority
?
3,074 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.
Suggested Courses

764 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