I´m trying to make a script which takes a single command-line argument. Then it looks on the argument and if its a directory name, it just prints that this directory exists. If its a file name, it prints out the file exists. Otherwise, it tries to create a directory with this name and tests whether it was successful and reports this on the standard output.
my code is:
while read argument; do
if [ $argument -d ]; then
echo "Directory exists"
elif [ $argument -e ]
echo "File exists"
else
mkdir $argument
if [ $argument -d]; then
echo "Directory was created"
else
echo "Error while creating the directory"
fi
fi
done
Then I run the code ./file_name.sh argument. If I run the code like this, I get an error on line 8, which is just "else". While is probably not necessary here, it was the first option how to accept an argument from the command line that came to my mind.
As you mentioned, you need single command line argument, So no need to loop
#!/bin/bash
if [[ -z "$1" ]]; then
echo "Help : You have to pass one argument"
exit 0
fi
if [[ -d "$1" ]]; then
echo "Directory exists"
elif [[ -f "$1" ]]; then
echo "File exists"
else
mkdir "$1"
if [ -d "$1" ]; then
echo "Directory was created"
else
echo "Error while creating the directory"
fi
fi
if [ -d $1 ]; then
echo "Directory exists"
elif [ -e $1 ]; then
echo "File exists"
else
mkdir $1
if [ -d $1 ]; then
echo "Directory was created"
else
echo "Error while creating the directory"
fi
fi
I made up this solution thanks to the links provided, thank you.
Related
This is my code:
#!/bin/bash
if [[ -z $1 ]]; then
echo "No arguments passed: valid usage is script.sh filename"
else if [[ ! -f "$1" ]]; then
echo "file does not exists"
else
for i in {558..2005};
do
if [[ ! -d "/abc" ]]; then
mkdir /abc
fi
mkdir /abc/xyz$i
cp $1 /abc/xyz$i/$1
done
fi
my error: can anyone please help me i do not know what to do? I do not know where I am making mistake?
./script.sh: line 17: syntax error: unexpected end of file
Use elif instead of else if.
Syntax of if in bash:
if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi
Instead of a single if statement with an elif clause, you nested a second if statement in the else clause of the first, but only terminated the second one. Your code, reformatted to highlight the issue, is
if [[ -z $1 ]]; then
echo "No arguments passed: valid usage is script.sh filename"
else
if [[ ! -f "$1" ]]; then
echo "file does not exists"
else
for i in {558..2005};
do
if [[ ! -d "/abc" ]]; then
mkdir /abc
fi
mkdir /abc/xyz$i
cp $1 /abc/xyz$i/$1
done
fi
Notice the lack of a second fi which would terminate the outer if statement.
Using elif, your code becomes
if [[ -z $1 ]]; then
echo "No arguments passed: valid usage is script.sh filename"
elif [[ ! -f "$1" ]]; then
echo "file does not exists"
else
for i in {558..2005};
do
if [[ ! -d "/abc" ]]; then
mkdir /abc
fi
mkdir /abc/xyz$i
cp $1 /abc/xyz$i/$1
done
fi
The elif clause doesn't require a closing fi; it is implicitly terminated by the following else clause.
Just looking for some help with my mounting shell script, wondering if anyone could advice me on how to make it check for the directory at the mount point exists and is empty, or is created by the script if it does not exist
#!/bin/bash
MOUNTPOINT="/myfilesystem"
if grep -qs "$MOUNTPOINT" /proc/mounts; then
echo "It's mounted."
else
echo "It's not mounted."
mount "$MOUNTPOINT"
if [ $? -eq 0 ]; then
echo "Mount success!"
else
echo "Something went wrong with the mount..."
fi
fi
Your use of grep will return any mountpoint that contains the string /myfilesystem in... e.g: both of these:
/myfilesystem
/home/james/myfilesystem
Prefer to use something more prescriptive like the following:
mountpoint -q "${MOUNTPOINT}"
You can use [ to test if a path is a directory:
if [ ! -d "${MOUNTPOINT}" ]; then
if [ -e "${MOUNTPOINT}" ]; then
echo "Mountpoint exists, but isn't a directory..."
else
echo "Mountpoint doesn't exist..."
fi
fi
mkdir -p will create all parent directories, as necessary:
mkdir -p "${MOUNTPOINT}"
Finally, test if a directory is empty by exploiting bash's variable expansion:
[ "$(echo ${MOUNTPOINT}/*)" != "${MOUNTPOINT}/*" ]
It's also a good idea to run scripts with some level of 'safety'. See the set built-in command: https://linux.die.net/man/1/bash
-e Exit immediately if a pipeline (which may consist of a single simple command), a
list, or a compound command (see SHELL GRAMMAR above), exits with a non-zero
status.
-u Treat unset variables and parameters other than the special parameters "#" and "*"
as an error when performing parameter expansion.
In full: (note bash -eu)
#!/bin/bash -eu
MOUNTPOINT="/myfilesystem"
if [ ! -d "${MOUNTPOINT}" ]; then
if [ -e "${MOUNTPOINT}" ]; then
echo "Mountpoint exists, but isn't a directory..."
exit 1
fi
mkdir -p "${MOUNTPOINT}"
fi
if [ "$(echo ${MOUNTPOINT}/*)" != "${MOUNTPOINT}/*" ]; then
echo "Mountpoint is not empty!"
exit 1
fi
if mountpoint -q "${MOUNTPOINT}"; then
echo "Already mounted..."
exit 0
fi
mount "${MOUNTPOINT}"
RET=$?
if [ ${RET} -ne 0 ]; then
echo "Mount failed... ${RET}"
exit 1
fi
echo "Mounted successfully!"
exit 0
Here is how can you check directory exist and it is empty:
if [ -d /myfilesystem ] && [ ! "$(ls -A /myfilesystem/)" ]; then
echo "Directory exist and it is empty"
else
echo "Directory doesnt exist or not empty"
fi
I am trying to write a simple program that checks if a given filename refers to a directory. But I keep getting an error saying that "bad interpreter: no such file or directory" when I run ./isdir.sh (This is the name of the script).
here is my code:
#!bin/sh
if [[ $# -ne 1 ]];then
echo "Usage: $0 dir"
exit 1
fi
dir="$1"
dirname = "$1"
if [[ ! -d "$dir" ]]; then
echo "Error: directory $dir not found."
exit 2
fi
if [[ -d "$dirname" ]]; then
echo "$dirname is a directory."
exit 3
ALSO, IMPORTANT QUESTION:
How do I handle input values that contain spaces in them?
You could do this:
#!/bin/bash
if [[ $# -ne 1 ]]; then
echo "Usage: $0 dir"
exit 2
fi
dir="$1"
rc=1
if [[ -d "$dir" ]]; then
# it is a directory
rc=0
elif [[ -e "$dir" ]]; then
echo "Error: '$dir' is not a directory"
else
echo "Error: directory '$dir' does not exist"
fi
exit "$rc"
Don't need the dirname variable.
The script is safe for directories that have white spaces or wild cards in their name as we have enclosed the variable in double quotes.
Exit code of 2 is a Unix standard for invalid argument error; exit 1 is all other errors. However, you can change the exit codes as per your need.
See this related post for more perspectives around this problem: Check if a directory exists in a shell script
This is what I have been trying and it is unsuccessful. If I wanted to check if a file exists in the ~/.example directory
FILE=$1
if [ -e $FILE ~/.example ]; then
echo "File exists"
else
echo "File does not exist"
fi
You can use $FILE to concatenate with the directory to make the full path as below.
FILE="$1"
if [ -e ~/.myexample/"$FILE" ]; then
echo "File exists"
else
echo "File does not exist"
fi
This should do:
FILE=$1
if [[ -e ~/.example/$FILE && ! -L ~/example/$FILE ]]; then
echo "File exists and not a symbolic link"
else
echo "File does not exist"
fi
It will tell you if $FILE exists in the .example directory ignoring symbolic links.
You can use this one too:
[[ -e ~/.example/$FILE && ! -L ~/example/$FILE ]] && echo "Exists" || echo "Doesn't Exist"
Late to the party here but a simple solution is to use -f
if [[ ! -f $FILE]]
then
echo "File does not exist"
fi
A few more examples here if you're curious
I'm reading a string from file by a shell script.
it goes like this:
count = 0
while read LINE
do
count++
if [ "$LINE" == "NONE" ]
then
echo "state is NONE"
else
if [ "$LINE" == "PLAYING" ]
then
echo "state is PLAYING"
fi
fi
done<$FILENAME
this is what I read from the file, and how I handle it, now I want to do something else if the file not found, is there anyway to do that?
for example:
if[ file not found]
then
do something
fi
if [ -f path_to_file ]
then
echo "file was found"
else
echo "file was not found"
fi
You'd better start you script with a condition like:
if [ ! -f /your/file ]; then
echo "file not found"
else
...
proceed with your `while`, etc.
...
fi
Since you are trying to read the file, then maybe you should test to see if the file exists, and if you have read access:
if [[ ! -f $FILENAME ]] || [[ ! -r $FILENAME ]]
then
# do stuff
fi
The -f tests to see if $FILENAME is a regular file, -r tests to see if you (the current user) has read access.