Why is this bash script not changing path? - bash

I wrote a basic script which changes the directory to a specific path and shows the list of folders, but my script shows the list of files of the current folder where my script lies instead of which I specify in script.
Here is my script:
#!/bin/bash
v1="$(ls -l | awk '/^-/{ print $NF }' | rev | cut -d "_" -f2 | rev)"
v2=/home/PS212-28695/logs/
cd $v2 && echo $v1
Does any one knows what I am doing wrong?

Your current script makes no sense really. v1 variable is NOT a command to execute as you expect, but due to $() syntax it is in fact output of ls -t at the moment of assignment and that's why you have files from current directory there as this is your working directory at that particular moment. So you should rather be doing ordinary
ls -t /home/PS212-28695/logs/
EDIT
it runs but what if i need to store the ls -t output to variable
Then this is same syntax you already had, but with proper arguments:
v1=$(ls -t /home/PS212-28695/logs/)
echo ${v1}
If for any reason you want to cd then you have to do that prior setting v1 for the same reason I explained above.

Related

How to store or capture variable from one directory to another and execute with cut command in Linux

I need to do as following, firstly I can read a server name from the text file line by line and do a grep command on it. The main task is to find the whole FQDN server name using only the hostname in the NB logs path and do other commands further. Here is the script I wrote but I am beginner in shell scripting and need your help. What do I missing here because I was thinking How to save a variable's value into another directory path and use it there? If I manually run these commands in command line it can give me what I do need it., thank you all..
#!/bin/sh
echo
echo "Looking for the whole backup client names?"
for server in `cat ./List_ServerNames.txt`;
do
cd /usr/openv/netbackup/logs/nbproxy/
return_fqdn=`grep -im1 '$server' "$(ls -Art /usr/openv/netbackup/logs/nbproxy/ | tail -n1 | cut -
d : -f8)" | cut -d " " -f 6 | cut -c -36 | cut -d "(" -f 1`
echo $return_fqdn
done
The output of script is down below:
Looking for the whole backup client names?

Checking file existence in Bash using commandline argument

How do you use a command line argument as a file path and check for file existence in Bash?
I have the simple Bash script test.sh:
#!/bin/bash
set -e
echo "arg1=$1"
if [ ! -f "$1" ]
then
echo "File $1 does not exist."
exit 1
fi
echo "File exists!"
and in the same directory, I have a data folder containing stuff.txt.
If I run ./test.sh data/stuff.txt I see the expected output:
arg1=data/stuff.txt
"File exists!"
However, if I call this script from a second script test2.sh, in the same directory, like:
#!/bin/bash
fn="data/stuff.txt"
./test.sh $fn
I get the mangled output:
arg1=data/stuff.txt
does not exist
Why does the call work when I run it manually from a terminal, but not when I run it through another Bash script, even though both are receiving the same file path? What am I doing wrong?
Edit: The filename does not have spaces. Both scripts are executable. I'm running this on Ubuntu 18.04.
The filename was getting an extra whitespace character added to it as a result of how I was retrieving it in my second script. I didn't note this in my question, but I was retrieving the filename from folder list over SSH, like:
fn=$(ssh -t "cd /project/; ls -t data | head -n1" | head -n1)
Essentially, I wanted to get the filename of the most recent file in a directory on a remote server. Apparently, head includes the trailing newline character. I fixed it by changing it to:
fn=$(ssh -t "cd /project/; ls -t data | head -n1" | head -n1 | tr -d '\n' | tr -d '\r')
Thanks to #bigdataolddriver for hinting at the problem likely being an extra character.

Can I use a variable in a file path in bash? If so, how?

I'm trying to write a small shell script to find the most recently-added file in a directory and then move that file elsewhere. If I use:
ls -t ~/directory | head -1
and then store this in the variable VARIABLE_NAME, why can't I then then move this to ~/otherdirectory via:
mv ~/directory/$VARIABLE_NAME ~/otherdirectory
I've searched around here and Googled, but there doesn't seem to be any information on using variables in file paths? Is there a better way to do this?
Edit: Here's the portion of the script:
ls -t ~/downloads | head -1
read diags
mv ~/downloads/$diags ~/desktop/testfolder
You can do the following in your script:
diags=$(ls -t ~/downloads | head -1)
mv ~/downloads/"$diags" ~/desktop/testfolder
In this case, diags is assigned the value of ls -t ~/downloads | head -1, which can be called on by mv.
The following commands
ls -t ~/downloads | head -1
read diags
are probably not what you intend: the read command does not receive its input from the command before. Instead, it waits for input from stdin, which is why you believe the script to 'hang'. Maybe you wanted to do the following (at least this was my first erroneous attempt at providing a better solution):
ls -t ~/downloads | head -1 | read diags
However, this will (as mentioned by alvits) also not work, because each element of the pipe runs as a separate command: The variable diags therefore is not part of the parent shell, but of a subprocess.
The proper solution therefore is:
diags=$(ls -t ~/downloads | head -1)
There are, however, further possible problems, which would make the subsequent mv command fail:
The directory might be empty.
The file name might contain spaces, newlines etc.

Use "Touch -r" for several files with automator

I use "MacOS X Yosemite (10.10.4)"
I've converted video mts files to mov files using QuickTime, but the new file created doesn't preserve original Creation Date.
fileA.mts --> Creation Date: 07/02/2010 10:51
fileA_converted.mov --> Creation Date: Today 8:35
I'd like to change the Creation Date attribute of several files, using the date of the original files. I know I can do this by using Terminal "Touch" command in order to this:
touch -r fileA.mts fileA_converted.mov
touch -r fileB.mts fileB_converted.mov
As I have more than 200 files to change Creation Date, is it possible to automate this using automator Script Shell action, or any other way?
Like this in the bash shell - which is what you get in Terminal (untested):
#!/bin/bash
for orig in *.mts; do
# Generate new name from old one
new="${orig/.mts/_converted.mov}"
echo touch -r "$orig" "$new"
done
Save the above in a file called doDates and then type this in the Terminal
chmod +x doDates # make the script executable
./doDates # run the script
Sample output
touch -r Freddy Frog.mts Freddy Frog_converted.mov
touch -r fileA.mts fileA_converted.mov
At the moment it does nothing, but run it, see if you like what it says, and then remove the word echo and run it again if all looks ok.
Execute below command when we have all original and converted files in same folder
ls | grep ".mts" | awk -F. '{print $0" "$1"_converted.mov"}' | xargs touch -r
when we have different folder run below command on path where .mts files are present and add absolute path before $1 just like I have added /home/convertedfiles/
ls | grep ".mts" | awk -F. '{print $0" /home/convertedfiles/"$1"_converted.mov"}' | xargs touch -r

bash script list files from given user

I have a problem with this one.
It is constantly returning me, not a directory, but is certainly is
#!/usr/local/bin/bash
DIR=$1
if [ -d "$DIR" ]; then
ls -1Apl /home/$DIR | grep -v /\$
else
echo "not a directory"
fi
One more thing, I need a little hint. I have to list files from a given user in a given directory, where I get both the user and directory as parameters.
Just suggestions, please.
Are you in the /home directory when you run this? If not, you may want to change it to:
if [ -d "/home/$DIR" ]; then
to match the ls command. This is assuming you're running it with something like myscript pax to examine the /home/pax directory, which seems to be the case.
And if you want to only list those files in there owned by a specific user, you can use awk to only print those with column 3 set to the desired value ($usrnm), something like:
ls -1Apl /home/$DIR | grep -v /\$ | awk -v user=${usrnm} '$3==user{print}{}'
You're not testing for the existence of the same directory as you're trying to list - maybe you mean -d "/home/$DIR"? Or from your requirement, do you have two parameters?
user="$1"
dir="$2"
# and then examine "/home/$user/$dir"

Resources