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"
Related
So I am trying to write a script in which I will pass folder name as command line argument. The script will search a the files within that folder if it exists and output all the details. My script is as shown
for entry in "$1"/*
do
data = $(stat $entry)
echo "${data}"
done
when I run it gives me following output.
./test.sh: line 3: data: command not found
I have taken the idea from here: Stat command
Can someone help what is wrong
I have tried variations like making $entry to entry and echo $data
Your main issue is that you are putting space characters around the equal operator, you must remove them:
data=$(stat $entry)
It is also good practice to pass variables between quotes in case your folder or any of the filenames contains whitespaces:
data=$(stat "$entry")
I assume that you are storing the value into a variable because your intent is to use it into an algorithm. If you just want to call stat to list your files, you can simply call:
stat "$1"/*
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"
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
My /home/s/project/bin/FileOperation.sh file is:
#!/bin/ksh
/home/s/project/Param/Param_File.config
current=$(dirname $0)
echo $current
echo 'hello'
echo $RootDir
echo $message
My /home/s/project/Param/Param_File.config file is:
RootDir=/home/s/project
message=hello
When I execute the above shell script I get below output:
.
hello
How can I get the values of $RootDir and $message printed?
Is there any shortcut way to import a config file instead of writing /home/s/project/Param/Param_File.config in shell script?
Try this:
#!/bin/ksh
. /home/s/project/Param/Param_File.config
current=$(dirname $0)
echo $current
echo 'hello'
echo $RootDir
echo $message
The dot (.) tells the shell to execute the file as a script in the current environment (thus the environment changes are effective). See e.g. here. You must ensure the executed file is a valid script.
Without the dot(.) the script is run in a new process, which can't alter it's parent environment.
The relevant part of the documentation:
. name [arg ...]
If name is a function defined with the function name
reserved word syntax, the function is executed in the current
environment (as if it had been defined with the name() syntax.)
Otherwise if name refers to a file, the file is read in its entirety
and the commands are executed in the current shell environment. The
search path specified by PATH is used to find the directory containing
the file. If any arguments arg are specified, they become the
positional parameters while processing the . command and the original
positional parameters are restored upon completion. Otherwise the
positional parameters are unchanged. The exit status is the exit
status of the last command executed.
Originally I would like to sync directory (with all files and subdirectories) given in parameter in bash script.
I found this post: How can I recursively copy a directory into another and replace only the files that have not changed? which explains how to use rsync in similar case.
My bash script is quite simple and listed below:
#!/bin/bash
echo -e "Type the project to be deployed: \c "
read project
echo -e "* Deploying: $project *"
echo -e "Sync: /var/repo/released/$project"
echo -e " /var/www/released/$project"
rsync -pr /var/repo/released/$project /var/www/released/$project
As a result it copies everything within /released (there are many directories in there, let's say -projects-).
I would like to copy (sync) only project given in parameter.
Could you please advice how to do this?
When you call the script without an argument (which most likely is what you're doing since you interactively read the project name into the variable $project), the positional parameter $1 remains empty. Therefore the script will rsync the entire content of /var/repo/released/.
You need to replace $1 with $project in your script. Also, I'd recommend to put double quotes around the paths to avoid problems due to spaces in a directory name.
rsync -pr "/var/repo/released/$project" "/var/www/released/$project"