How to determine if sym link exists [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I'm trying to determine if a symbolic link exists. I thought that -L or -f would do it, but it doesn't seem to be working.
VHOST="/etc/apache2/sites-available/vhost.local";
if [ ! -L VHOST ]; then
ln -s /home/user/Ubuntu\ One/htdocs/vhosts/vhost.local VHOST;
a2ensite vhost.local;
echo " -vhost.local":
fi
It should create the sym link if there isn't already one....

According to the TEST(1) manpage:
-h FILE
FILE exists and is a symbolic link (same as -L)
-L FILE
FILE exists and is a symbolic link (same as -h)
-h or -L should do the trick. However, you are not testing against the variable $VHOST but the literal VHOST. That's the error.
So, I suppose you meant to say:
if [ ! -L "$VHOST" ]; then
...
Additionally you're forgetting a $ in ln -s:
ln -s /home/user/Ubuntu\ One/htdocs/vhosts/vhost.local "$VHOST";

Related

File exists in shell script [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I want to check file exists in a folder. I have a test.sh and test.json files in a folder. In the test.sh file I have scripts for checking whether the test.json file exists. The below code i have used for checking the existance. I have used the ls command and it shows the file exists but the code I have used is not finding the file and it prints file not exists.
can you pls help me whether I have missed anything here.
ls
File=test.json
echo "$FILE"
if [ -f "$FILE" ]; then
echo "file exists"
else
echo "file not exists"
fi
bash is case sensitive, you need to use same case in declaration and usage.
e.g.
FILE=test.json
echo "$FILE"
if [ -f "$FILE" ]; then
...

Automated script install using bash [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Improve this question
I'm trying to script install the below, how can I answer "y" at the prompt within the command
wget -O - mic.raspiaudio.com | sudo bash
I have tried the usual but this wont work
echo "y" | wget -O - mic.raspiaudio.com | sudo bash
Disclaimer: The solution below works for script that have a non-interactive switch.
I believe the echo won't work on this because it's not writing to the /dev/tty that the bash spawned. You can do it using the default feature bash provides.
From the man page:
-c If the -c option is present, then commands are read from the first
non-option argument command_string. If there are arguments after the
command_string, the first argument is assigned to $0 and any remaining
arguments are assigned to the positional parameters.
If you use -c option with bash, you can supply args to script that will run and those will be placed as mentioned in the man page. eg:
bash -c "script" "arg0" "arg1" .... The arg0 will be placed in $0 and arg1 will be placed in $1 and so on.
Now, I don't know if this can be generalized, but this solution will only work if there is a non-interactive mode in the script.
If you see the script it has the following function:
FORCE=$1
confirm() {
if [ "$FORCE" == '-y' ]; then
true
else
read -r -p "$1 [y/N] " response < /dev/tty
if [[ $response =~ ^(yes|y|Y)$ ]]; then
true
else
false
fi
fi
}
And is used as :
if confirm "Do you wish to continue"
then
echo "You are good to go"
fi
So, if we can set the $1 to "-y" it won't ask for a confirmation, We will try to do that same by:
$ bash -c "$( wget -qO - mic.raspiaudio.com)" "dummy" "-y"
This should work for the script, provided it does not have any other interactive options. I have not tested the original script by my own minimal script and it seems to work. eg:
$ bash -c "$(wget -qO - localhost:8080/test.sh)" "dummy" -y
You are good to go
$ bash -c "$(wget -qO - localhost:8080/test.sh)"
Do you wish to continue [y/N] y
You are good to go

Creating a Script that will create user accounts (using useradd) from a .txt file of usernames [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to create a script that will create user accounts (using useradd) from a .txt file of usernames. Please help me because my professor has not been the best of help.
The text file's name is users.txt
The only in the file is usernames, we are suppose to set up a default password and have it where they must change it on the next logon. We are also supposed to add them into a group called interns.
Here is what I have so far:
#!/bin/bash
for i in users.txt
do
sudo echo $i
sudo useradd $i -m -d /home/$i -s /bin/bash $i -G sudo interns $i
passwd = echo "AIST2330password" | passwd -stdin $i
sudo passwd -e $i
echo "User must change password when they come back"
done
This line:
for i in users.txt
should be:
for i in $(cat users.txt)
Your code is iterating over the literal string users.txt, not the contents of the file.
And this line:
passwd = echo "AIST2330password" | passwd -stdin $i
should be:
echo "$i:AIST2330password" | chpasswd
because the --stdin option to passwd has been deprecated.

Linux script listing directory [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
I need a linux shell script for listing everything in a directory.
But I want to output also if it is a directory or a file
Can someone help me?
Use ls -l
$ ls -l /tmp
drwx------ 2 me me 4096 Nov 21 10:16 buu <--- dir
-rwx------ 2 me me 4096 Nov 21 10:16 foo <--- file
lrwxrwxrwx 1 me me 1 Nov 21 11:16 b -> a <--- link
The first character on the first block shows the file type:
- Regular file
b Block special file
c Character special file
d Directory
l Symbolic link
n Network file
p FIFO
s Socket
Can someone give a script example?
Here is a script that lists all directories and files:
for i in *; do
[[ -d "$i" ]] && echo "d-$i"
[[ -f "$i" ]] && echo "f-$i"
[[ -b "$i" ]] && echo "b-$i"
done
This is because OP wanted to do it via a script otherwise ls -l does the job.

How do you list all symlinks in a directory that has non-hanging links? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I would like to get a list of all symlinks within a directory that has valid links. In other words, I would like all the broken links to be discarded in my list.
In shell, [ -L "$f" ] && [ -e "$f" ] is true if and only if "$f" is the name of a symlink whose target exists. So:
for f in *; do
if [ -L "$f" ] && [ -e "$f" ]; then
# do something with "$f"
fi
done
(Never use the -a or -o options to test/[...]; they cannot be relied on to have sane precedence.)

Resources