Shell: read a file and echo it's contents to another file - shell

I have a Makefile that is supposed to echo aliases from another file to your local .zshrc file.
I need to read the contents of the file aliases.sh, and echo it's contents to ~/.zshrc, how is this possible?

cat aliases.sh >> ~/.zshrc

Related

permission denied on .gz files on linux

I have some .gz files, of which i want to run a script on. I need these files to remain in .gz format. When I run my script:
#!#bin#bash
for f1 in $(~/Desktop/hawkfiles17/NG*.fq.gz);
do
echo "${f1}"
done
I want to check the location of the files. The script returns:
bash: /home/amyhouseman/Desktop/hawkfiles1/NG0921017_EKDN210018957-1A_HN2MGDSX2_L2_1.fq.gz: Permission denied`
I have tried using:
chmod u+x /home/amyhouseman/Desktop/hawkfiles17/NG0921017_EKDN210018957-1A_HN2MGDSX2_L2_1.fq.gz, but bash returns:
bash: /home/amyhouseman/Desktop/hawkfiles17/NG0921017_EKDN210018957-1A_HN2MGDSX2_L2_1.fq.gz: cannot execute binary file: Exec format error
I'd be grateful if someone could help, I know that you can't execute .gz files, but I'm not sure what else i can do?
I did look through other posts before.
I want to check the location of the files.
You're shebang is incorrect, and several other small strings.
Here is your solution:
$ cat my_script.sh
#!/bin/bash
for item in $(ls ~/Desktop/hawkfiles17/NG*.fq.gz) ; do echo "$item" ; done

.sh file returned file path instead of file name

I am writing a .sh file to print the file names one by one. I have installed ubuntu in windows 10 and using the windows command prompt for executing below code. It is returning "E:/Official/Backups/GGG/*" instead of file names inside. I have also changed the EOL conversion to Unix(LF) by using notepad ++. please help.
#!/bin/bash
folder="E:/Official/Backups/GGG"
for entry in "$folder"/*
do
echo "$entry"
done
Running the script outputs:
$ bash test1.sh
E:/Official/Backups/GGG/*
Output of echo $-
himBHs
Output of ls -ld E:/Official/Backups/GGG
ls: cannot access 'E:/Official/Backups/GGG': No such file or directory
My bash in WSL does not recognize windows paths. If I want to access E:\Official\Backups\GGG I would have to use /mnt/e/Official/Backups/GGG.
I assume, the same goes for your WSL bash. Therefore the "path" E:/Official/Backups/GGG is just a non-existing directory and your observed behavior is to be expected. With bash's default settings a * just stays there as a literal if the directory does not exist or is empty. Example:
$ echo /dir/that/doesnt/exist/*
/dir/that/doesnt/exist/*
$ echo /dir/that/exists/but/is/empty/*
/dir/that/exists/but/is/empty/*
$ echo /dir/*
/dir/file1 /dir/file2 /dir/file3 ...
GGG folder is not exists. Please check and update with valid folder and try again.
#!/bin/bash
folder="E:"
for entry in "$folder"/*
do
echo "$entry"
done

how can I turn config ini file into system environment in bash?

I have config files like below
# this is sample config file like config.ini
APP_HOME=/usr/local/bin
DATABASE_DIR=/usr/local/database
Normally in order to be access as system environment, it shall use export in front
# this is sample config file like config.rc
export APP_HOME=/usr/local/bin
export DATABASE_DIR=/usr/local/database
And I can
$ source config.rc
$ echo "APP_HOME is $APP_HOME"
APP_HOME is /usr/local/bin
Now Which is the easiest way one line command to turn config file config.ini into system environment ? could be combine with sed/awk command
You can tell the shell to automatically export variables, if you really do need this to be exported.
set -a # turn on automatic export
source config.ini # execute all commands in the file
set +a # turn off automatic export
sed 's/^/export /g' config.ini > config.sh && source config.sh
The sed command add 'export ' to the beginning for each line of config.ini and then redirect the output to config.sh, then the source shell builtin read and execute the exports in the current shell environment.
To export the variables in that file after you source if:
export $(grep -oP '^\s*\K[_[:alpha:]]\w+(?==)' config.ini)
one line:
cat 1.ini | awk '{print "export "$0}'

Output the Names of Copied Files in Shell Script

I am working on a bash script to copy configuration files to their appropriate directories. I would like to output the name of the file that I have copied to the user. Here is what I have so far:
CONFIG_DIR="/home/stackoverflow/app/configs"
CONFIG_FILE="*.config"
cp $CONFIG_FILE $CONFIG_DIR
echo "$CONFIG_FILE was copied to $CONFIG_DIR"
The output I get from this is:
*.config was copied to /home/stackoverflow/app/configs
Let's assume that stackoverflow.config is the only .config file present in the directory where the script is running. I would like it to say:
stackoverflow.config was copied to /home/stackoverflow/app/configs
Either add -v to copy and have it print all the file names (one name per line) or change the echo to
echo $CONFIG_FILE was copied to $CONFIG_DIR
i.e. remove the double quotes (they are not necessary in this example).
Move the $CONFIG outside the quotes, in order to allow the shell to expand its wildcards:
echo $CONFIG_FILE "was copied to $CONFIG_DIR"
CONFIG_DIR="/home/stackoverflow/app/configs"
for file in *.config
do
cp $file $CONFIG_DIR
echo $file was copied to $CONFIG_DIR
done

Why aren't the BASH commands in for loop working

I have a simple code which is:
#!/bin/bash
#LaTex code generator for figures.
ls *.pdf > pdfs.file
ls *.ps > ps.file
pwd=$(pwd)
for i in {1..2}
do
# var=$(awk 'NR==$i' 'pdfs.file')
echo $pwd
echo $pwd > testfile
done
Why aren't the commands in the for loop working?
The $pwd isnt echoed neither is the testfile created.
I tried these commands without the for loop in a terminal and they work fine.
My bash file is made executable by chmod +x bashfile.sh
What I am trying to do is this:
Find pdfs or eps files and populate pdfs.file and eps.file with their file names.
Step through row by row and grab these file names and append to $pwd.
Then append $pwd$var to the include graphics command in latex.
I'm not sure what you're doing wrong, but this works fine for me:
for i in {1..2}; do
echo $PWD
echo $PWD > /tmp/testfile
done
echo "File contents: $(cat /tmp/testfile)"
This successfully returns the following:
/tmp
/tmp
File contents: /tmp
Did you write the bash file using a Windows editor? Maybe you have a problem with line terminators. Try dos2unix bashfile.sh.

Resources