Solved

shell script to loop and ask for a new filename

Posted on 2006-07-12
6
3,068 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
Comment Utility
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
Comment Utility
so I can add in addtional file names
0
 
LVL 1

Author Comment

by:urbanweasel
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Expert Comment

by:JJSmith
Comment Utility

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
Comment Utility
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
Comment Utility

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now