suppose we have "token=1234" content present in a filename. how to get the complete value 1234 and store it in variable called token in shell script
So that I can use $token to use it
If you're using Bash, eval is your friend.
eval "token=1234"
echo $token # should give you 1234
Also, can you explain what content present in a filename means? Are you trying to parse the file name or the file's content?
If it's the latter you can just execute your script like any other script:
./yourfile.sh
If the file contains just the line token=1234 then:
source filename
echo "$token"
Related
I want to write a bash script which copies the last line containing a particular string from a bunch of similarly named files to a new file.
For example I have three files: 1abc1.txt, 2abc2.txt and 3abc3.txt.
From these three files i want to extract the last line containing the term "pass" and write those extracted lines to a new file named "ABC.txt".
The following is the bash script I came up with: (pass.sh)
#!/bin/bash
grepline pass "$1" 1 > $2
Then I issued the following command:
./pass.sh *abc*.txt ABC.txt
But it doesn't create the ABC.txt file. Instead it scans for the string "pass" only in 1abc1.txt and then writes the output to 2abc2.txt .
I am supposing that my use of wild cards while issuing the command is not correct. Please can anyone suggest how to achieve what I want to do with the script?
The wildcards are expanded by the shell before your script is executed, so actually you execute
./pass.sh 1abc1.txt 2abc2.txt 3abc3.txt ABC.txt
If you need to pass wildcards to your script you should quote this argument, and then let the shell expand it within the script
./pass.sh '*abc*.txt' ABC.txt
and the script should contain
grepline pass $1 1 > $2
I created a .sh file that is supposed to run the cat command on a text file then replace part of the text and echo it back.
The file name that I'm running the cat command on is "New" with the content Hello and the executable script is:
alias message="cat ~/ll/New"
echo "${message//ello/X}
alias command does not assign a variable
So ${message) is empty
Probably you should use message as a command not as variable
I trying to append passed text as parameter to file using shell script
this is the code of shell
echo $1>>/etc/freeradius/mods-enabled/ldap
this shell will get the text to add it to the file ldap in /etc/freeradius/mods-enabled path , I call this shell in this format
# sh /etc/append.sh hello how are you man
but in this example the shell only get first word 'hello' and append it to file .how can I tell shell that all words are same variable and should insert to file
It depends on the shell you're using.
In bash the this script should work:
#!/bin/bash
echo "${#:1}">>/etc/freeradius/mods-enabled/ldap
Edit: As DTSCode pointed out in comments the :1 part in the script is redundant so this would be more correct:
#!/bin/bash
echo "$#" >> /etc/freeradius/mods-enabled/ldap
Then give the permissions to execute the file and call
/etc/append.sh hello how are you man
Or without execute permission call
bash /etc/append.sh hello how are you man
I am trying to pass argument in a shell script. This is what i have done:
#!/bin/bash
name=$1
echo $name
cd folder
/users9/test/test1/ggandhi/wmd/latest/test/resources/ + name
I want to append the value of name after the resources directory. Does anyone know how would I do it?
If $name corresponds to a command (script or binary) in that folder, the last line of your script should read like
/users9/test/test1/ggandhi/wmd/latest/test/resources/"$name"
This will execute the command given by $name which resides inside the folder /users9/test/test1/ggandhi/wmd/latest/test/resources.
Also, put double quotes around $1 when you assign it to name:
name="$1"
I have a curl script where one of the variables in the URL is actually the name of .sh file.
So, if file name is test.sh then in the script I would want curl www.example.com/test where it would grab the 'test' from the file name.
I'm able to obtain the actual name by using a combination of answers I see here: How do I know the script file name in a Bash script?.
However, being new to this I'm not exactly sure how it should be formatted and specifically how to output the variable.
Would appreciate any help
For your example:
# The name of the executable (with the .sh removed if it's there)
SCRIPT_NAME=`basename $0 .sh`
# Put the entire argument in quotes in case SCRIPT_NAME has a space in it.
# The curly braces aren't strictly necessary, but ensure that if you need to
# put anything immediately after, like "_test", it's not treated as part of the
# variable name.
curl "www.example.com/${SCRIPT_NAME}"
See example 5.1 here for an example of how to use variables, or section 3.2.2 here for a more in-depth description.
If I understand you correctly
for test.sh
#!/bin/bash
name=${s/"$(basename $0)"/".sh"} #ie name of scipt, no path, .sh removed
echo "DEBUG: $name"
curl "http://www.example.com/$name"