Shell script (assign to variable) not working [duplicate] - bash

This question already has an answer here:
bash script with variables not getting set [duplicate]
(1 answer)
Closed 2 years ago.
#!/bin/bash
if [ -f "$1" ] #parameter is file name, if i exists...
then
VARIABLE=`cat "$1"` #assign what's inside into VARIABLE
fi
I've tried bash -x script_01.sh "file" and the tracing works:
+ '[' -f file ']'
++ cat file
+ VARIABLE='Wednesday, November 4, 2015 04:45:47 PM CET'
But the problem is that when I echo $VARIABLE, it's empty. Do you guys have any tips on how to make it work? Thank you.

VARIABLE is set in the process that runs the script, not the process which called the script. If you want to set it in your current environment, you need to source the file instead.
. script_01.sh "file"

Related

Is there a way to see the action of a shell script as it is executed? [duplicate]

This question already has answers here:
How can I debug a Bash script? [closed]
(12 answers)
Closed 1 year ago.
I am pretty new here and learning programming.
I wonder if there is a way to see the action of a shell script as it is executed?
For example, I want to see how the values are inserted into the variables in this script.
#!/bin/bash
# Script: potenz2.sh (powers of two with a while loop)
i=0
j=1
while (( i < 12 ))
do
j=$((j * 2))
echo -n "$j"
i=$((i+1))
done
echo ""
Put set -x at the beginning of the script, or run it with that option
bash -x potenz2.sh

Script to get all file names in a directory [duplicate]

This question already has answers here:
Extract filename and extension in Bash
(38 answers)
Closed 2 years ago.
I need to get all file names for a directory
for file in "$1"/*;do
echo $file
done
gives me directory_name/file1_name, directory_name/file2_name.
However, I just want the file1_name
You can use parameter expansion to get only the filename before printing as follows:
for file in "$1"/*;do
file="${file##*/}"
echo $file
done
You can find more about parameter expansion over here : Shell Parameter Expansion (Bash Reference Manual)

bash - get directory a script is in when run from a different directory [duplicate]

This question already has answers here:
How do I get the directory where a Bash script is located from within the script itself?
(74 answers)
Closed 4 years ago.
If I'm in /home/whatever and I run, say, $ bash /home/folder/subfolder/script1.sh, how can I get /home/folder/subfolder into a variable? $0 or $(readlink -f $0) give me the whole path and filename, i.e. /home/folder/subfolder/script1.sh, which isn't what I'm after. Thanks in advance.
Use basename after getting the script name
dirname /home/folder/subfolder/script1.sh
outputs
/home/folder/subfolder
You may try this:
#!/bin/bash
p=$(dirname $0)
echo $p

what does the below shell script mean? [duplicate]

This question already has answers here:
What do $? $0 $1 $2 mean in shell script? [duplicate]
(2 answers)
Closed 5 years ago.
I have seen one shell script starting with the below code :-
#!/bin/bash
currfoldername=$1
cd $currfoldername
can anyone describe what does $1 mean here?
Thanks for your reply!!
$1 means the first argument given while executing the shell script.
Example -
# my_script.sh
#!/bin/bash
currfoldername=$1
cd $currfoldername
echo "in $currfoldername"
execute -
./my_script.sh my_folder
output -
# value of variable currfoldername is my_folder.
#cd to my_folder
in my_folder # echo statement

Get current directory of file after getting called by another bash script [duplicate]

This question already has answers here:
How do I get the directory where a Bash script is located from within the script itself?
(74 answers)
Closed 9 years ago.
So I have one bash script which calls another bash script.
The second script is in a different folder.
script1.sh:
"some_other_folder/script2.sh"
# do something
script2.sh:
src=$(pwd) # THIS returns current directory of script1.sh...
# do something
In this second script it has the line src=$(pwd) and since I'm calling that script from another script in a different directory, the $(pwd) returns the current directory of the first script.
Is there any way to get the current directory of the second script using a simple command within that script without having to pass a parameter?
Thanks.
I believe you are looking for ${BASH_SOURCE[0]}, readlinkand dirname (though you can use bash string substitution to avoid dirname)
[jaypal:~/Temp] cat b.sh
#!/bin/bash
./tp/a.sh
[jaypal:~/Temp] pwd
/Volumes/Data/jaypalsingh/Temp
[jaypal:~/Temp] cat tp/a.sh
#!/bin/bash
src=$(pwd)
src2=$( dirname $( readlink -f ${BASH_SOURCE[0]} ) )
echo "$src"
echo "$src2"
[jaypal:~/Temp] ./b.sh
/Volumes/Data/jaypalsingh/Temp
/Volumes/Data/jaypalsingh/Temp/tp/
Please try this to see if it helps
loc=`dirname $BASH_SOURCE`

Resources