Solved

shell script to loop and ask for a new filename

Posted on 2006-07-12
6
3,069 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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…
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.:

939 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

16 Experts available now in Live!

Get 1:1 Help Now