Unable to copy files using Shell Script [duplicate] - linux

This question already has answers here:
How to assign a glob expression to a variable in a Bash script?
(8 answers)
Wildcard within quotations
(3 answers)
Closed 8 months ago.
I am unable to copy files using Shell script. I validated in shellcheck.net no issues found in syntax.
It showing error as file not fount but I am using exact file name.
Below is my Shell script.
#! /bin/bash
export SOURCE_PATH="/home/la1122/export"
export SOURCE_TARGET="/home/la1122/export/backup"
FILE="*.dat"
cd $SOURCE_PATH
echo "Current location: $(pwd)"
cp "$FILE" "$SOURCE_TARGET"

Related

Bash Script: `cd` with command substitution [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Change the current directory from a Bash script
(17 answers)
How can I store a command in a variable in a shell script?
(12 answers)
Closed 2 years ago.
Summary:
I am trying to have a script that allows easy one-touch access to files that are generated by the crontab.
The hourly generated files go by the path as: /home/mouse/20210126/0900.
Of course as the date and time changes, the path will change like so:
/home/mouse/20210126/1000
/home/mouse/20210126/1100
/home/mouse/20210127/1000
/home/mouse/20210128/1300
I tried using an alias, but once .bashrc is loaded, it only takes the present date/time, which means that I cannot "refresh" the date and time.
So by using a script, I could update the date and time like so:
currentdate=$(date +%Y%m%d)
currenttime=$(date -d '1 hour ago' "+%H00")
collect=cd /home/mouse/$currentdate/$currenttime
echo $currentdate
echo $currenttime
$collect
However, I realized that the cd script is not working because, from my understanding, it works on a subshell and once the script has finished executing, it does not affect the main shell.
I tried source / . but I get /home/mouse/20210126/1000 is a directory.
What is the recommended action I should take to resolve this?

How to fix command not found in Linux shell scripting [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
why subtraction return - symbol
(2 answers)
Closed 2 years ago.
I have a Linux shell script with the below code
#! /bin/bash
echo $BASH
name = Mark
echo $name
When I run the script, I'm getting an error:
./my_script.sh: line 3: =: command not found
What am I doing wrong?
Note: I'm using Kali Linux.
In shell, you need to write:
echo $BASH
name=Mark
echo $name
Note there are no spaces around = when setting a variable. The shell usually interprets name = Mark as calling the command name with arguments =and Mark, hardly what you intend. It also seems that name somehow expands to nothing (an alias?), thus the confusing message about command =.

How to get a full path to a file by name in Linux [duplicate]

This question already has answers here:
How to obtain the absolute path of a file via Shell (BASH/ZSH/SH)?
(22 answers)
Closed 4 years ago.
I was given a task to write script that works similar to "which" command in terminal. Here is what I already wrote:
#! /bin/bash
FILE=$1
for i in $PATH
do
if [[ "$i" -eq "FILE" ]]
then
echo …
Here I need to get a full path to a file that has been found. How could i get it? Thanks in advice.
If you're willing to use Python, use Terminal's locate program using subprocess, like:
import subprocess
def find_filepath(file_name):
outcome = (subprocess.Popen(["locate", file_name], stdout=subprocess.PIPE).communicate()[0]).decode()
return outcome.split("\n")
This should fetch a list of Absolute file path.

How to get full path of a file from another directory in shell script? [duplicate]

This question already has answers here:
How do you normalize a file path in Bash?
(24 answers)
Closed 6 years ago.
I've a shell script at
/home/abc/xyz/test.sh
I've a file at
/home/def/ghi/jkl/lmn/foo.txt
I'm running the scrip from /home as I've set my PATH variable to point to /home/abc/xyz/
I'm passing the relative path of foo.txt to my script like so test ./def/ghi/jkl/lmn/foo.txt
I want to capture the full path of foo.txt into a variable in my script, any pointers?
Tried https://stackoverflow.com/a/5265775 and https://stackoverflow.com/a/9107028/4468505 but in vain.
foobar=`readlink -f <passed in relative path to foo.txt>`

How to move to other directory with bash? [duplicate]

This question already has answers here:
How can I cd into a directory using a script?
(4 answers)
Closed 7 years ago.
My script
#!/bin/bash
for file in *.ats; do
mv $file /home/holmes/procmt
done
cd /home/holmes/procmt
All files are moved but I want to change my current directory to /home/holmes/procmt and nothing happens.WHY?
If i run script ./pch.sh,I stay in the same shell.
The shell running the script moved, but the shell you were typing in when you started the script stayed put. If you precede the name of your shell script with . (dot space) it will run in 'your' shell.

Resources