[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
# 8  
Old 11-21-2011
That is why I suggested you to copy my script...
You chmod 775 sample0001, then run the code and see what it does!
I am waiting for your comments...
# 9  
Old 11-21-2011
what it does? ok, it displays script, think because you used :
PHP Code:
#!/bin/sh -x 
then at the end script waits for something to be typed in
but since you have not change it there is still error with missing desitnation
PHP Code:
======================
/
home/andre/Documents/Scripts
$2
idr :  /home/andre/Documents/Scripts/Copy
cp
missing destination file operand after `/home/andre/Documents/Scripts/Copy'
Try 
`cp --help' for more information.
  copied to directory /home/andre/Documents/Scripts/Copy 
---------- Post updated at 12:44 PM ---------- Previous update was at 12:41 PM ----------

in other words : these script is bad :d
# 10  
Old 11-21-2011
Code:
======================
+ echo  Start of  sample0001                       # + sign = execution ok! (that is the -x for debug mode )
 Start of  sample0001 
+ echo ======================
======================
+ dir=/home/vbe/Documents/Scripts/Copy
+ pwd
+ ls
+ echo /home/vbe
/home/vbe
+ [ -d /home/vbe/Documents/Scripts/Copy ]
+ mkdir Copy
+ cp /home/vbe/Documents/Scripts/Copy
Usage:  cp [-f|-i] [-p] [-S] [-e warn|force|ignore] source_file target_file
        cp [-f|-i] [-p] [-S] [-e warn|force|ignore] source_file ... target_directory
        cp [-f|-i] [-p] [-S]  -R|-r [-e warn|force|ignore] source_directory ... target_directory
+ echo line 18 -   copied to directory /home/vbe/Documents/Scripts/Copy 
line 18 -   copied to directory /home/vbe/Documents/Scripts/Copy  # here you should have $2 value... (but empty so nothing to display)
+ echo next
next
+ read kyb              # This is to make the loop wait for you to press a key to continue...
q

# 11  
Old 11-21-2011
ok, read kyb = wait for keyboard, learning new stuff , good,

now a hint from you would be appreciated, something that I can work on (just remember what forum are we now Smilie )
should I use functions ? how ?
can I read from #ls all parameters ? how?
I want to have it done asap so your help appreciated!
# 12  
Old 11-21-2011
Can you see what is missing and how to correct now? (Before I leave till tomorrow...)
# 13  
Old 11-21-2011
yes I will work on it today and have it done by tomorrow.
if I finish I will post what I have, and if I wont get it done I will post another questions Smilie

---------- Post updated at 03:53 PM ---------- Previous update was at 12:58 PM ----------

changed the code Smilie
but still are not 100% happy with it

HTML Code:
#!/bin/bash
dir=/home/andre/Documents/Scripts/Copy
files=$(pwd | ls -m )
counter= #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

while [ $counter -lt $numberfies ];
do
cp ${file}s ${dir}
let counter=counter+1
until [ $counter=numberfiles ] do
shift 1

#cp $files $dir/$files
#echo " $files has been copied "
shift doesnt realy work here, how can I make it working any ideas? how to use shift to use it to stop copying files ....
thanks

---------- Post updated at 08:10 PM ---------- Previous update was at 03:53 PM ----------

i come up with another script Smilie
but again it doesnt work properly

tried to use " shift " but something is wrong here
Code:
#!/bin/bash
dir=/home/andre/Documents/Scripts/Copy
files=$(pwd | ls -m )
counter= #counter set to number of files counted with wc -c
numberfiles=$(ls -m | wc -w )
shift 2


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

while [ $files -eq $dir | ls -m ];
do
echo " next file is  "
cp -i " $files $dir "
shift 2
done

---------- Post updated at 08:13 PM ---------- Previous update was at 08:10 PM ----------

here I created loop :d
but maybe someone can create one properly working script out of those Smilie
I am off for today, hope someone can help me here Smilie
Code:
#!/bin/bash
dir=/home/andre/Documents/Scripts/Copy
files=$(pwd | ls -m )
counter= #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                # line 10
echo " $dir exists so now will copy files "
fi

until [ $counter - le  $numberfies ]
do
echo " next file is  "
counter=$(( counter+1 ))
done


Last edited by me.; 11-21-2011 at 07:18 PM..
# 14  
Old 11-22-2011
I'm trying with the little time I have today (busy...) to work out your request from the beginning ( You may not like that...) in order to analyse and start giving you what you need to achieve your task.
So ""want to create script that takes name of directory and all files and will copy each file to new directory." Is this still the case? If yes this is why in my script I introduced you the read command.
Q: What do you want really? Have your script to accept 2 parameters: fromDIR ToDIR?

If this is the case (before doing anything else!) you would have to test
1) if fromDIR exist, if not ask to enter the path to directory to copy
2) does ToDIR exist if not can you create it if yes OK if not: What will you do?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

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
Login or Register to Ask a Question