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

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?

Related

Unable to copy files using Shell Script [duplicate]

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"

How to execute text from stdout in bash [duplicate]

This question already has answers here:
Bash script - variable content as a command to run
(7 answers)
Closed 1 year ago.
So I have a command that's like
cmd1|cmd2|...|cmdN|execute
The output of cmdN is one line that I want execute to execute as if I copy pasted the output of cmdN into the terminal myself. I've tried to replace execute with bash, with $, and I tried to use xargs. (I'm still kinda confused on each of the options I've tried).
What's the simple answer here that's gonna make me wanna delete this post?
If you need to execute it in the current shell, use the eval command:
eval "$(cmd1|cmd2|...|cmdN)"
If it can be executed in a subshell, pipe to bash:
cmd1|cmd2|...|cmdN | bash

clarification about bash script [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 6 years ago.
I am new to bash scripting. This might be obvious to many. please bear with me.
I have a shell script as follows:
#!/bin/bash
echo `pwd`
cd /home/foo/bar
echo `pwd`
Let's say I am currently in dir : /home/foo1
If I execute the above script it prints:
/home/foo1
/home/foo/bar
But once the script completes execution, I have seen that it still remains in dir /home/foo1
I have also seen some scripts where there are explicit commands to reset the working dir using 'cd -' command.
If bash executes all the lines in the script as commands, why does it again reset the working dir?
When you are running an interactive session of bash, and from it, you execute a script (e.g. ./myscript.sh), then bash creates a new bash process to execute the script. Initially, that process get copy of the same environment as the original process (e.g. the current working directory, or environment variables), but if the script modifies the environment somehow, this changes only affect the new process, not the original one. So when the scripts exits, you go back to the original process which retains the original environment. So it is not possible to modify the current directory of the original shell from a script.
As a side note, the following line
echo `pwd`
does not make much sense. You either have to do echo $PWD or simply pwd.

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.

Use return value of a shell command as a value in shell script? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Capturing multiple line output to a bash variable
For example I want to run ls command and take the return list as a value kept in a array in shell script.
Something like
run
#ls
fileA
fileB
fileC
kept this return list in a variable that keeps a array
variable A = ["fileA","fileB","fileC"];
I cannot give the exact notation for code since I do not know how to write shell script. After I learn this, I 'll.
#!/bin/bash
variableA=$(ls)
echo $variableA
That should be your shell script assuming that you have bash
Then all you'd need to do is chmod +x shell_script to make it executable.
If you use ls > contents.file the result of ls is saved to a file called contents.file.
Remember, > rewrites the entire file while >> appends to the last line.
variableA=$(ls)
echo "$variableA"

Resources