I'm having trouble taking in a path where to run the script as a command line argument, test is it exists, then changing to that path to perform work. Here what I'm trying:
#!/bin/bash
scriptpath=$1
if [ $# -lt 1 ]
then
echo "Usage: script.sh <directory_name>"
fi
if [ -d scriptpath ]
then
# work......
else
echo "Directory does not exist"
fi
Change this:
if [ -d scriptpath ]
to this:
if [ -d $scriptpath ]
Also, I recommend making use of "", so that your script still behaves properly when the argument contains weird characters. (Unix allows spaces, newlines, asterisks, even control characters inside filenames.) So:
scriptpath="$1"
...
if [ -d "$scriptpath" ]
Related
I'm a new bee to shell and would ask for your kindly help.
I created a small script to remove the extra " which is extra when copying Windows file into Linux.
But my script has below issue:
1st error is PARAM is not recognized. (I copied from others)
PARAM=-linux -brnce -cdw: Command not found.
Illegal variable name.
2nd. When I try to ignore PARAM, there is new error
if: Expression Syntax.
The script is as below:
'''
#file name: removeQuote.sh
#!/bin/bash
PARAM="-linux -brnce -cdw"
indent $PARAM "$#"
if [ $? != 0 ]
then
exit 1
fi
if [ $# -ge 3 ] #check if there is output file
then
file_path = $3
else
file_path = $1
fi
vim -e -s -c "%s/\"//g" -c "wq" $file_path
echo "ok, all extra \" removed"
'''
apps="http:git.abc.com";
cluster-ui="http:git.xyz.com";
customer-ui="http:git.xxx.com";
SERVICE=$1;
My requirement is if I pass service name as a 'apps' then I need to clone the $apps url.
Here
if [ $Service -eq apps ]
not think a good approach as my repo url might get increased so more and more loop will come
Any suggestions?
The $ sign assigns the input argument, so we're getting first input if it matches the below variable, so do what you want inside if condition.
#!/bin/bash
apps="http:git.abc.com";
clusterui="http:git.xyz.com";
customerui="http:git.xxx.com";
#SERVICE=$1;
#Store global
repo=''
# if empty parameter is passed
if [ $# -lt 1 ] ; then
echo "Parameters Need"
exit 1
fi;
# for search the correct parameter
if [ $1 = "apps" ]; then
repo=$apps
elif [ $1 = "cluster-ui" ] ; then
repo=$clusterui
elif [ $1 = "customer-ui" ] ; then
repo=$customerui
else
echo "Not found"
fi;
echo $repo
Note just repeat elif [ ] ;then for more entries or think!
how to access run this file like this sh ./file.sh apps just replace apps with yours. make sure you have permission to execute the file if you don't have, give it to permission like below
chmod 766 file
now run the shell script sh ./file.sh clusterui
'Case statement' would suit here more than if ladder
I have this shell script, and the folder does exist, but it always have no as result?
#!/bin/bash
FOLDER=/Library/Application\ Support/ESET/esets
if [ -d “$FOLDER” ];
then
echo yes
else
echo no
fi
thanks!
if [ -d “$FOLDER” ];
The type of quote marks you're using are not valid for the shell. Use "$FOLDER" instead.
I wanted to create the alias for the a bash script that make use of wild card as an argument. When I am trying to make use of the alias cmd its not giving the required output.
The usage of the alias cmd will be log_list /tmp/abc*
#usage log_list /tmp/abc*
alias log_list=`sh scriptname $1`
#here is the script code
for file_name in $* ; do
if [ ! -d $file_name ] && [ -f $file_name ] ; then
#do some processing ...
echo $file_name
fi
done
Aliases don't handle arguments via $1 and the like; they just prepend their text directly to the rest of the command line.
Either use:
alias log_list='sh scriptname' # note that these are single-quotes, not backticks
...or a function:
log_list() { sh scriptname "$#"; }
...though if your script is named log_list, marked executable, and located somewhere in the PATH, that alias or function should be completely unnecessary.
Now, that said, your proposed implementation of log_list also has a bunch of bugs. A cleaned-up version might look more like...
#!/bin/sh
for file_name in "$#" ; do
if [ ! -d "$file_name" ] && [ -f "$file_name" ] ; then
#do some processing ...
printf '%s\n' "$file_name"
fi
done
Hello i am currently optimizing some code i have and am now wondering why this snippet of code does not work.
what is supposed to happen is when i copy the file i have to type in a destination for the copy if i type a destination that doesn't exist then the script will create a directory and copy that file to it.
this is what i have and i am wondering why it is not working
echo "Current Directory "
ls -a;
echo -n "Please Enter file name to Copy: "
read fileToCopy
echo -n "Enter Destination for Copy: ~/ "
read location
if [ -d $location ]
then
cp $fileToCopy $location
echo "File Successfully Copied to ~/ $location "
elif [ !-d $location ]
then
mkdir $location
cp $fileToCopy $location
echo "$location was created and the File was Copied to It! "
echo -n "Press Enter to Continue: "
else
echo "That file Does Not Exist! "
fi
Two issues:
One:
elif [ !-d $location ]
You need a space between ! and -d.
Two:
Quote your variables.
It's always good to error check. What if the user didn't enter a directory name or a file to copy?
In shell, a null variable will cause an error in a statement like this:
if [ $dir -eq foo ]
then
…
fi
That's because the shell will directly substitute the value and then interpret the line. If $dir is null, the statement will be:
if [ -eq foo ]
then
…
fi
This isn't valid. If you had quotes around $dir:
if [ "$dir" -eq foo ]
then
…
fi
You'd get this:
if [ "" -eq foo ]
then
…
fi
Which is valid.
If you're using bash it's preferable to use the double square brackets:
if [[ $dir -eq foo ]]
then
…
fi
This is a special improved syntactic test that will handle null variables and variables with spaces without having to use quotes.
The ! is a special shell operator that negates the return value of a command/statement. For the shell to understand the operator, you must make sure there is white space on either side of it. Also test the output of your mkdir statement to make sure it worked. And use the -p parameter. This will create parent directories.
if [[ ! -d "$dir" ]]
then
if ! mkdir -p "$dir"
then
echo "Could not create dir '$dir'" 1>&2
exit 2
fi
fi
Note that I always put quotes around my variable and I use the if statement to test to see if my command succeeded or not. After all, the user might have tried to create a directory where I don't have write permission.
One more secret trick. You can use set -xv to turn on shell script debugging. This will print out each statement as written and then as the shell interprets it. To turn it off use set +xv.