I am a beginner in shell script and have to code a script to simulate the functionality of mv command of linux without actually using the mv command in my script. Please help me I will be very thankful !
If you want to simulate the mv command you need to copy the file that you want to "move" to the destination folder then you have to delete the old one.
Here is a little script:
#!/bin/bash
from=$1
to=$2
echo "Copy from $1 to $2"
cp $1 $2
echo "Removing the old file $1"
rm $1
echo "Done."`
Hope that will help you.
Related
This question already has answers here:
How to make a programme executable anywhere in the SHELL
(4 answers)
How do I run a shell script without using "sh" or "bash" commands?
(13 answers)
Closed 1 year ago.
I am learning shell scripting, and I have written two scripts one that copies many files at once to a folder and if the folder does not exist it creates it, works like a charm! You may use it :D and the other one that moves them, I mean cuts and pastes :)
to copy:
#!/bin/bash
if [ ! -e "$2" ]
then
mkdir $2
fi
if [ "$3" ]; then
cp -rn $1/*.$3 $2
echo "Copying *.$3 done!"
else
cp -rn $1/*.* $2
echo 'Copying done!'
fi
to move:
#!/bin/bash
if [ ! -e "$2" ]
then
mkdir $2
fi
if [ $3 ]; then
mv -i $1/*.$3 $2
echo "Moving *.$3 done!"
else
mv -i $1/*.* $2
echo 'Moving done!'
fi
I would like to be able to use them like any other shell command (eg. ls, ping, cd...) everywhere in my system. How can I achieve that?
Thanks!
You need to
Name each script file what you'd like to call it when you run it (without the extension), e.g. copymany and movemany
Place the copymany and movemany files in a folder, perhaps ~/bin
Add the folder to your $PATH environment variable, e.g. export PATH=$PATH:$HOME/bin, in your .bashrc or .zshrc
I'm looking to check if a variable file is in another directory, and if it is, stop the script from running any farther. So far I have this:
#! /bin/bash
for file in /directory/of/variable/file/*.cp;
do
test -f /directory/to/be/checked/$file;
echo $?
done
I ran an echo of $file and see that it includes the full path, which would explain why my test doesn't see the file, but I am at a loss for how to move forward so that I can check.
Any help would be greatly appreciated!
Thanks
I think you want
#! /bin/bash
for file in /directory/of/variable/file/*.cp ; do
newFile="${file##*/}"
if test -f /directory/to/be/checked/"$newFile" ; then
echo "/directory/to/be/checked/$newFile already exists, updating ..."
else
echo "/directory/to/be/checked/$newFile not found, copying ..."
fi
cp -i "$file" /directory/to/be/checked/"$newFile"
done
Note that you can replace cp -i with mv -i and move the file, leaving no file left behind in /directory/of/variable/file/.
The -i option means interrogate (I think), meaning if the file is already there, it will ask you overwrite /directory/to/be/checked/"$newFile" (or similar) to which you must reply y. This will only happen if the file already exists in the new location.
IHTH
The command basename will give you just the file (or directory) without the rest of the path.
#! /bin/bash
for file in /directory/of/variable/file/*.cp;
do
test -f /directory/to/be/checked/$(basename $file);
echo $?
done
Here is my code:
export ALLOW_RPM_UPGRADE=True
path='/opt/rpm/latest/'
echo $1
file=$1
echo $file
dest=${path}${file}
echo $dest
cp $source $dest
Problem:
The three echo statements are printing the same value.
The third one is not appending path to $dest variable.
This is because I had created shell script on windows machine. Before executing it on linux we need to covert it through DOS2Unix utility. OR Just create shell script on Linux and save.
After this it is working fine.
Thanks
I'm not a pro in shell scripting, thats why I ask here :).
Let's say I got a folder. I need a script that monitors that folder for new files (no prefix name of files is given). When a new file gets copied into that folder, another script should start. Has the second script processed the file successfully the file should be deleted.
I hope you can give me some ideas on how to achieve such script :)
Thank you very much in advance.
Thomas
Try this:
watcher.sh:
#!/bin/bash
if [ -z $1 ];
then
echo "You need to specify a dir as argument."
echo "Usage:"
echo "$0 <dir>"
exit 1
fi
while true;
do
for a in $(ls -1 $1/* 2>/dev/null);
do
otherscript $a && rm $a #calls otherscript with the file a as argument and removes it if otherscript returned something non-zero
done
sleep 2s
done
Don't forget to make it executable
chmod +x ./watcher.sh
call it with:
./watcher.sh <dirname>
try inotify(http://man7.org/linux/man-pages/man7/inotify.7.html)
or you may need to install inotify-tools (http://www.ibm.com/developerworks/linux/library/l-ubuntu-inotify/) to use it by shell.
Is it possible to save last entered value of a variable by the user in the bash script itself so that I reuse value the next time while executing again?.
Eg:
#!/bin/bash
if [ -d "/opt/test" ]; then
echo "Enter path:"
read path
p=$path
else
.....
........
fi
The above script is just a sample example I wanted to give(which may be wrong), is it possible if I want to save the value of p permanently in the script itself to so that I use it somewhere later in the script even when the script is re-executed?.
EDIT:
I am already using sed to overwrite the lines in the script while executing, this method works but this is not at all good practice as said. Replacing the lines in the same file as said in the below answer is much better than what I am using like the one below:
...
....
PATH=""; #This is line no 7
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )";
name="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")";
...
if [ condition ]
fi
path=$path
sed -i '7s|.*|PATH='$path';|' $DIR/$name;
Someting like this should do the asked stuff :
#!/bin/bash
ENTERED_PATH=""
if [ "$ENTERED_PATH" = "" ]; then
echo "Enter path"
read path
ENTERED_PATH=$path
sed -i 's/ENTERED_PATH=""/ENTERED_PATH='$path'/g' $0
fi
This script will ask user a path only if not previously ENTERED_PATH were defined, and store it directly into the current file with the sed line.
Maybe a safer way to do this, would be to write a config file somewhere with the data you want to save and source it . data.saved at the begining of your script.
In the script itself? Yes with sed but it's not advisable.
#!/bin/bash
test='0'
echo "test currently is: $test";
test=`expr $test + 1`
echo "changing test to: $test"
sed -i "s/test='[0-9]*'/test='$test'/" $0
Preferable method:
Try saving the value in a seperate file you can easily do a
myvar=`cat varfile.txt`
And whatever was in the file is not in your variable.
I would suggest using the /tmp/ dir to store the file in.
Another option would be to save the value as an extended attribute attached to the script file. This has many of the same problems as editing the script's contents (permissions issues, weird for multiple users, etc) plus a few of its own (not supported on all filesystems...), but IMHO it's not quite as ugly as rewriting the script itself (a config file really is a better option).
I don't use Linux, but I think the relevant commands would be something like this:
path="$(getfattr --only-values -n "user.saved_path" "${BASH_SOURCE[0]}")"
if [[ -z "$path" ]]; then
read -p "Enter path:" path
setfattr -n "user.saved_path" -v "$path" "${BASH_SOURCE[0]}"
fi