[Solved] take name of directory and files as variables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] take name of directory and files as variables
# 15  
Your first attempt partially corrected so it can work a little ( I left the errors where I could...)
Code:
#!/usr/bin/ksh
dir=/home/vbe/Documents/Scripts/Copy
files=$(pwd | ls -m )
counter=0 #counter set to number of files counted with wc -c
numberfiles=$(ls -m | wc -w )

echo " files in these folder are $files "
if [ ! -d $dir ]; then
   mkdir Copy
else
   echo " $dir exists so now will copy files "
fi
read kyb

while [ "$counter" -lt "$numberfiles" ]
do
   #cp ${file}s ${dir}
   echo ${file}s ${dir}
   let counter=counter+1
   #until [ $counter=numberfiles ] do
   #shift 1
   read kyb
done
#cp $files $dir/$files
#echo " $files has been copied "

Do run it it sh -x scriptname...
I let you explain to me the errors you see.
This will help you set your mind for the next step
I admire people trying to learn as fast as possible but we are humans, and to understand what you are doing - it takes time and asks to be well organized in the head:
Knowing what you want to achieve is one thing - How to achieve it is another...
In computing science it means being capable of every thing to a level of elementary tasks (you can then replace by a command...). Normally when not too sure you would start by thinking algorithmic using a pseudo-language or pseudo-code to write down
the different processes involved.
Only after years of practical experience can you bypass that step (because you did it in your mind...).
The pseudo-code helps you understand what you are trying and points out things you forget for example...
# 16  
I am waiting to see what you come up with and hope I am not wasting my time...(I prepared a script of a possible solution (accepting one argument only but tests its validity before entering loop...)
# 17  
first sorry!
I didnt see page 3 with your last posts and it has been a while since I checked it last time so you probably think you wasted your time, so sorry about that.

second, thanks
for your help but I am getting nowhere with it any way Smilie each time I sit to it and try modify - getting different code different errors,
would appreciate if you correct that code so it work corectly, if you find time ofcourse, I believe it takes less time for you to correct it than for me trying to figure out. I know I learn less if get working code but would appreciate it.

thanks
# 18  
You are too impatient and trying too many things at the time - Makes debugging really hard job!
Lesson 1 : Modify ONLY one thing at a time - Once issue solve only can you now pass to the next one...

I will have to remember which server I used...
This User Gave Thanks to vbe For This Post:
# 19  
yes you are right, I am annoyed I wasted time with that code ( and I understand if you are annoyed with my late and useless responses) and it still dont work.
will take a strong coffe and try fix it again later...

Good Luck, vbe!
# 20  
I know I wrote a script for you... But where is it???? Smilie
# 21  
Found it!
(That's a pint of Guinness for me!)
OK
The script is called copy2:
Code:
#@(#) copy2 v1.0   :Copy all files from current directory to directory given as parameter.
# usage   : copy2 ~/Dirname
#
############           Test the parameter validity             ##############
alias ll='ls -l'
USAGE="usage   : copy2 ~/Dirname"
echo "Current directory is : "$PWD
if
    [ "$#" -eq 1 ]
then
   Dirname=$1
   if [ -d $Dirname ]
   then
      echo " $Dirname exist... will start copying: "
   else
      echo "No $Dirname... Will try to create $Dirname..."   
      sleep 3
      mkdir -p ~/$Dirname || exit 97
      echo "Directory $Dirname  created!"
      echo "Press Enter to continue";read kyb
   fi
elif
     [ "$#" -gt 1 ]
  then
     echo !!$# Only one directory accepted for argument !!
     echo $USAGE
     exit 99
  else
     echo copy2: Copy all files from current directory to directory given as parameter.
     echo $USAGE
     echo !!Entered arg:$#! -  give a Directory for argument !!
     exit 98
fi
# exit
############        you get here only if above is good         ##############
#

for i in $(ls)
do
   echo "File $i is being copied..."
   cp $i ~/$Dirname/.
   echo " Done: "
   ll ~/$Dirname/$(ls)
   #echo "Press Enter to continue";read kyb
   #Uncomment above to see each separate copy of loop being done
done
echo "Finished!"
echo "Copied $(ll |wc -l) files "
#exit

This is something you will have to keep as model to what should be in a good script...
Good learning!
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #740
Difficulty: Medium
FORTRAN II first appeared in 1958.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[Solved] How to find particular files ina directory?

Hi, I am trying to write a script to find some files in a directory Example: if i have files like 2014-02-01_aaaa.txt 2014-02-01_bbbb.txt 2014-02-01_cccc.txt 2014-02-01_dddd.txt and some other files how can i just check to see if there four files exits or not i tried some thing like this... (5 Replies)
Discussion started by: vikatakavi
5 Replies

2. Shell Programming and Scripting

[SOLVED] nawk FS using pipe read variables from file

I have a file data_1.out which contains: 1|abc mail|mail subject|mail body 2|def mail|mail subject|def mail body I am trying to read the variables from data_1.out and use them to print to 2 different files based on the id (first_column) The problem is I am not able to read the file... (8 Replies)
Discussion started by: sol_nov
8 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Writing a loop to changing the names of files in a directory

Hi, I would like to write a loop to change the names of files in a directory. The files are called data1.txt through data1000.txt. I'd like to change their names to a1.txt through a1000.txt. How do I go about doing that? Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

4. Shell Programming and Scripting

[solved] Question for using variables outside a while loop

I want to get newvar outside the while any ideas? while read myline; do var=${myline} newvar1=$(let "$var") done echo $newvar1 I found it its ok now Thank you! (0 Replies)
Discussion started by: sanantonio7777
0 Replies

5. Shell Programming and Scripting

[Solved] Working with date (add minutes using variables)

Dear all, today I'm scratching my head with a simple (I believe) issue. Working with date is quite simple, so if I Need to add some seconds to current time, I'll use: date --date='+30 seconds' +"%Y-%m-%d %H:%M:%S"But, how to pass the value to add from a variable? I tried the following without... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

6. Shell Programming and Scripting

Moving files from one directory to another based on 2 date variables

Hi All, I am currently coding for a requirement(LINUX OS) where I am supposed to move a file (Lets Call it Employee.txt) from Directory A to Directory B based on 2 date fields as below, Date_Current = 20120620 Date_Previous = 20120610 Source Directory : /iis_data/source Target... (11 Replies)
Discussion started by: dsfreddie
11 Replies

7. Shell Programming and Scripting

[Solved] Bash test 2 variables to see if ones greater by n

Experts, I have a bash shell script that generates 2 variables that have the current minute and a minute from a log file. Can someone please show me the best way to test if the minutes stray by 5. So basically if: This is ok: Last Fitting Min ============= 02 Current Minute =============... (2 Replies)
Discussion started by: jaysunn
2 Replies

8. Shell Programming and Scripting

[Solved] Look for strings and use variables

Hello guys, First of all, thanks for taking the time to read my post. So, here I have a file from my honeypot which record IP addresses and web pages visited. I would like to manipulate it in order to create Snort signatures and ACLs. But I am having troubles to extract the src IP address and... (6 Replies)
Discussion started by: Benou
6 Replies

9. UNIX for Dummies Questions & Answers

[SOLVED] Delete files and folders under given directory

I have a requirement to delete the files and folders under a given directory. my directory structure is like this.. Data | A(Directory) |_PDF(Directory)----pdf files |_XML()Directory --xml files |--files | B(Directory) |_PDF(Directory)----pdf files |_XML()Directory --xml files ... (1 Reply)
Discussion started by: ramse8pc
1 Replies

10. Shell Programming and Scripting

[solved] merging two files and writing to another file- solved

i have two files as file1: 1 2 3 file2: a b c and the output should be: file3: 1~a 2~b 3~c (1 Reply)
Discussion started by: mlpathir
1 Replies

Featured Tech Videos