bash

create unix alias using a python3 script

百般思念 提交于 2021-02-05 09:06:09
问题 Well, as we all known, creating an alias in a terminal shell is quite easy: ZZ:~ zhangzhao$ alias c='uname' ZZ:~ zhangzhao$ c Darwin ZZ:~ zhangzhao$ But now I want to do the same thing through a Python3 script. I've checked the ref manual and found these sort of command work can be solved using subprocess module. Then I write the script below: import subprocess subprocess.call(["alias", "c=\'uname\'"]) But note that this operation will not take effect to the shell you are currently using,

Count unique values in a bash array

假装没事ソ 提交于 2021-02-05 08:56:51
问题 I have an array ${sorted[@]} . How can I count the frequency of occurrence of the elements of the array. e.g: Array values: bob jane bob peter Results: bob 2 jane 1 peter 1 回答1: The command (IFS=$'\n'; sort <<< "${array[*]}") | uniq -c Explanation Counting occurrences of unique lines is done with the idiom sort file | uniq -c . Instead of using a file, we can also feed strings from the command line to sort using the here string operator <<< . Lastly, we have to convert the array entries to

Bash Script to let user choose from a list or enter their own

核能气质少年 提交于 2021-02-05 08:49:27
问题 I have a simple script which i want to list some pre-filled choices or for the user to enter a number to correlate with a build This is what I have currently. read -p "Please select from the list below or enter the build number you would like to download " build case {build} in Latest) build=lastSuccessful break;; *) break;; esac The problem is that is doesn't provide a list for the user to choose. Ideally it would look something like this Build Choices 1) Latest 3) Successful 5) pick a build

Trimming a part of file extension for all the files in directory - Linux

巧了我就是萌 提交于 2021-02-05 08:45:15
问题 I have a requirement in which I want to trim the file extension for all the files contained in a directory: - The file will be like; America.gz:2170 Europe.gz:2172 Africa.gz:2170 Asia.gz:2172 what I need is to trim the :2170 and :2172 from all the files, so that only the .gz extension remains. I know that with the help of below SED code, it is possible for all the entries in a file, however I need for all the files in a directory: - **sed 's/:.*//' file** Any bash or awk code to fix this will

How to run multiple curl requests in parallel with multiple variables

瘦欲@ 提交于 2021-02-05 08:44:38
问题 Set Up I currently have the below script working to download files with curl , using a ref file with multiple variables. When I created the script it suited my needs however as the ref file has gotten larger and the data I am requesting via curl is takes longer to generate, my script is now taking too much time to complete. Objective I want to be able to update this script so I have curl request and download multiple files as they are ready - as opposed to waiting for each file to be

How to run multiple curl requests in parallel with multiple variables

落花浮王杯 提交于 2021-02-05 08:44:26
问题 Set Up I currently have the below script working to download files with curl , using a ref file with multiple variables. When I created the script it suited my needs however as the ref file has gotten larger and the data I am requesting via curl is takes longer to generate, my script is now taking too much time to complete. Objective I want to be able to update this script so I have curl request and download multiple files as they are ready - as opposed to waiting for each file to be

add time units to a date without timezone

佐手、 提交于 2021-02-05 08:36:06
问题 I write these in Linux bash date -d "2018-08-21 02:00:00" +'%y-%m-%d %T' and it prints 2018-08-21 02:00:00 But when I write these date -d "2018-08-21 02:00:00 +1 hour" +'%y-%m-%d %T' it prints 2018-08-21 07:30:00 instead of 2018-08-21 03:00:00 It adds my timezone to the date . How can I ignore timezone when I'm adding time units? 回答1: What is happening is that the +1 is interpreted as the timezone UTC+1h . So it will convert your input date from UTC+1 to your local time-zone and then still

Permission denied on cat via shell script [duplicate]

一个人想着一个人 提交于 2021-02-05 08:18:58
问题 This question already has answers here : How do I set a variable to the output of a command in Bash? (14 answers) Closed 4 years ago . I have an issue with running cat in shell script on a log file which is in ~/bin/rclone_sync_ACD.log. This is the line in the shell script: RESULT=cat $LOGFILE | tail -1 But when running the script I get: ./rclone_sync: line 63: /Users/pjburnhill/bin/rclone_sync_ACD.log: Permission denied In terminal, if I type cat $LOGFILE | tail -1 , it gives the right

Permission denied on cat via shell script [duplicate]

点点圈 提交于 2021-02-05 08:18:06
问题 This question already has answers here : How do I set a variable to the output of a command in Bash? (14 answers) Closed 4 years ago . I have an issue with running cat in shell script on a log file which is in ~/bin/rclone_sync_ACD.log. This is the line in the shell script: RESULT=cat $LOGFILE | tail -1 But when running the script I get: ./rclone_sync: line 63: /Users/pjburnhill/bin/rclone_sync_ACD.log: Permission denied In terminal, if I type cat $LOGFILE | tail -1 , it gives the right

macOS Mojave version 10.14.1 bash-3.2 expr: syntax error

こ雲淡風輕ζ 提交于 2021-02-05 08:13:29
问题 I meet such error: expr: syntax error and my script is: #!/bin/bash echo `expr index "sarasara" a` how to fix it. 回答1: $ man expr gives this at the end: According to the POSIX standard, the use of string arguments length , substr , index , or match produces undefined results. In this version of expr , these arguments are treated just as their respective string values. In other words, macOS' expr does not support index . 来源: https://stackoverflow.com/questions/53701873/macos-mojave-version-10