1

I am adding a variable that I want to be exported every time I run a terminal window. The problem is that it points to a file or directory that is not made at this time. But I want the bash to load and export the variable anyways because when I use it the file/folder will be there.

the variables

export ports=$(awk -F "/" 'BEGIN {ORS=","} /\/tcp/ {print $1}' nmap/initial.nmap | sed 's/,$//')
export portsf=$(awk -F "/" 'BEGIN {ORS=","} /\/tcp/ {print $1}' nmap/full.nmap | sed 's/,$//')

error messages:

awk: cmd. line:1: fatal: cannot open file `nmap/initial.nmap' for reading (No such file or directory)
awk: cmd. line:1: fatal: cannot open file `nmap/full.nmap' for reading (No such file or directory)

Is there any way to make this work through the bashrc or am I going to have make an alias that will export the variable I want each terminal session?

thanks for your time.

6
  • The command substitution runs immediately; it's not deferred until you actually want the value of ports. You'll have to wait until nmap/*.nmap exist to define the variables. Commented Jun 19, 2020 at 16:19
  • oh okay thanks!, i will have to set my exports to aliases and use them that way after I create the nmap/*.nmap. edit: alias --> echo "export string so at the very least copy and paste" thanks! Commented Jun 19, 2020 at 16:22
  • I would recommend functions rather than aliases. Commented Jun 19, 2020 at 16:24
  • How would I do name function in the bashrc? Commented Jun 19, 2020 at 16:32
  • How would I do name function in the bashrc? would it be something like this? function ports() { export ports.... } export -f ports Commented Jun 19, 2020 at 16:38

2 Answers 2

1

Just add the following function to your .bashrc.

set_ports () {
    awk 'F "/" 'BEGIN {ORS=","} /\/tcp/ {print $1}' "$1" | sed 's/,$//'
}

Once the required files are availble, you can run

ports=$(set_ports nmap/initial.nmap)
portsf=$(set_ports nmap/full.nmap)
export ports ports

You can omit the last line if you don't actually need to export the variables (which I suspect is the case).

Sign up to request clarification or add additional context in comments.

1 Comment

thats what i have
0

You could also check for existence of the files in your .bashrc and execute the exports command only if they exist. Something like this should work:

[ -f nmap/initial.nmap ] && export ports=$(awk -F "/" 'BEGIN {ORS=","} /\/tcp/ {print $1}' nmap/initial.nmap | sed 's/,$//')
[ -f nmap/full.nmap ] && export portsf=$(awk -F "/" 'BEGIN {ORS=","} /\/tcp/ {print $1}' nmap/full.nmap | sed 's/,$//')

what is after '&&' will be evaluated only if what is before '&&' exits with exit code 0. Concretely here for the first line for example, if the file 'nmap/initial.nmap' exists, you will indeed export the 'ports' variable and not otherwise.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.