Shell script to create directory from any random directory - shell

I am currently in /Desktop on my ubuntu system and I want to create a directory named vip inside /Documents/subd. Please not that Documents and Desktop are at same level. But the crux of this question is that I have to write a shell script such that it can create the requied directory from any directory of the system, no matter where it is situated.
I have tried concatenating $home with the required directory path!! But it is not working.
mkdir $home."/Documents/subd/vip"
I need to run this inside /Desktop or any other directory.
Please guide me!!

This should do the job:
mkdir "$HOME/Documents/subd/vip"
You just had some minor errors in your command.

Related

Command not found in ssh but is in direcory

I am using an ssh account that connects to an external server, i have downloaded through guix some software like samtools and bedtools but when i try to use them in my directory it gives me this error:
-bash: samtools: command not found
In my direcory, however, there is the directry guix.profile and if I go into the bin folder of this, I have everything I downloaded.
What am I doing wrong?
Thank you
enter image description here
To run a file from the shell you need two things:
The shell must find the file
Being in the same directory does not enable the shell to find the file. You have to either supply an absolute or relative path the file, or have the directory in your PATH environment variable
In simplest terms this means instead of
$ samtools
try
$ ./samtools
The relative path tells the shell it wants that file
To run it from another directory, either use the whole absolute path, e.g. /home/yourname/samtools , or move the file into a directory that is on your $PATH
The file needs to be executable
If the file is not executable you will need
$ chmod +x ./samtools

Airflow Bash Operator : Not able to see the output

I am a newbie to Airflow and trying to create a simple task of executing a bash file(whose job is just to create a directory). I have given the full path of the bash file to be executed (with a space at the end) in bash_command. However, upon triggering the DAG from the UI, I see no errors in the log as well as no folder created with the name specified in the bash file.
Can someone please help me fix the issue?
When BashOperator executes, Airflow will create a temporary directory as the working directory and executes the bash command. When the execution finishes, the temporary directory will be deleted.
To keep the directory created from the bash command, you can either
specify an absolute path outside of the working directory, or
change your working directory to a place outside of the temporary directory.
I am creating a test directory in the Airflow home directory.
p = BashOperator(
task_id='create_dir',
bash_command='pwd; mkdir $AIRFLOW_HOME/test; ls -al',
)

Unable to create /root/.config/<app> programmatically

I've built a script that places an icon in the launcher to open a program as the root user. This script also adds NOPASSWD to the user's configuration for this specific app in /etc/sudoers, however the one part of the script that refuses to work is the creation of the profile in /root/.config/<app>. I can create this manually, using the same mkdir command, but when I place the same command in the script it returns no such file or directory. I have replicated this behaviour a number of times, including on a clean install.
Is there some form of protection that disallows the ability to automate the creation of this directory? Or am I missing something about hidden folders in Linux?
I assume you are doing this when the .config dir does not exist yet.
mkdir /root/.config/<app>
Try this :
mkdir -p /root/.config/<app>
This will create any missing parent directory to the full path you provide.

how to make the bash script portable?

I have written a bash script which is dependent on current folder structure,
What should I do to make it runnable in any other folder(become portable)?
let me explain more: I want the script to delete all the files in current directory that the script is running,
or to delete all the files that are in the parent folder,
what is the solution?
cd "$(dirname "$0")"
Add this to the top of your script to have it cd to the directory the script is installed at. Then you can use relative paths and they'll be relative to the script dir and not the user's pwd.
If for whatever reason dirname isn't available, here's an equivalent statement that uses basename instead.
cd "${0%%/$(basename "$0")}"
Don't use absolute paths in your script.
we need more information to give you a proper answer...BUT some tips if you need to make it portable are to store any files that you need on a network share and simply make a new folder off of / such as /temporaryScriptFolder/ and mount the network share to that empty folder, allowing you easy access to any resources that are necessary.

Cd in shell script not working

First off I am very very new to shell scripting. I am trying to write a script that takes in one parameter and then copies a folder in a different directory naming it using the parameter. This is the current code that I have:
#!/bin/sh
cd /var/www/html/fbplugin/chrome
sudo mkdir temp/$1
sudo cp -rf "/var/www/html/fbplugin/chrome/fbplugin" "/var/www/html/fbplugin/chrome/temp/$1"
When I run this code it says can't cd to /var/www/html/fbplugin/chrome. I'm not sure why it is saying this because I know the directory exists. I have copied the line directly and it works in terminal. If anyone could help me out that would be great.
If it matters in order to run the script I am typing "sh build.sh"
If that directory really exists, then you must have executed that script with a different user (cron, webserver, etc).
Check the rights for that directory.
I don't know why you're getting the error about cd, but it looks like you could just use absolute paths throughout. That would solve the larger problem of the script working correctly.

Resources